A Tour of Go Channels
2024-08-25 05:48:12
Channels are a typed conduit through which you can send and receive values with the channel operator, <-
.
ch <- v // Send v to channel ch.
v := <-ch // Receive from ch, and
// assign value to v.
(The data flows in the direction of the arrow.)
Like maps and slices, channels must be created before use:
ch := make(chan int)
By default, sends and receives block until the other side is ready. This allows goroutines to synchronize without explicit locks or condition variables.
package main import "fmt" func sum(a []int, c chan int) {
sum :=
for _, v := range a {
sum += v
}
c <- sum //send sum to c
} func main() {
a := []int{, , , -, , } c := make(chan int)
go sum(a[:len(a)/], c)
go sum(a[len(a)/:], c)
x, y := <-c, <-c fmt.Println(x, y, x + y)
}
最新文章
- 谈谈黑客攻防技术的成长规律(aullik5)
- Direct3D 10学习笔记(三)——文本输出
- java编译后字节码解析
- php的几种运行模式CLI、CGI、FastCGI、mod_php
- 项目Windows服务安装命令:
- SQL Server常用函数
- easyui给select控件绑定change事件
- C# 订单流水号生成
- bootstraptable表格基本
- cocos2dx之WebView踩过的坑(android返回键处理问题)
- 记一个SwipeMenuListView侧滑删除错乱的Bug
- Java基础系列--02_运算符和程序的语句
- HTTPS请求
- Pycharm 远程调试
- java中获取远程ip的一个坑
- 7F - 无限的路
- Oracle数据库over函数的使用
- macOS下appstore提示未能完成该操作的解决办法
- ansible的高级应用-roles
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
热门文章
- hbase集群 常用维护命令
- 李洪强漫谈iOS开发[C语言-013]-常量
- android 内部存储相关知识点: getfilestreampath getDir 子文件夹
- png24是支持Alpha透明的。。。。。。
- *[topcoder]JumpFurther
- Qt: 界面中使用中文(三种方法,QApplication::translate可指定编码)
- Unable to resolve target 'android-8'类似错误的解决办法
- 安装Hadoop系列 — 导入Hadoop源码项目
- hdr_beg(host) hdr_reg(host) hdr_dom(host)
- Java 简单的加密解密算法