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)
}

最新文章

  1. 谈谈黑客攻防技术的成长规律(aullik5)
  2. Direct3D 10学习笔记(三)——文本输出
  3. java编译后字节码解析
  4. php的几种运行模式CLI、CGI、FastCGI、mod_php
  5. 项目Windows服务安装命令:
  6. SQL Server常用函数
  7. easyui给select控件绑定change事件
  8. C# 订单流水号生成
  9. bootstraptable表格基本
  10. cocos2dx之WebView踩过的坑(android返回键处理问题)
  11. 记一个SwipeMenuListView侧滑删除错乱的Bug
  12. Java基础系列--02_运算符和程序的语句
  13. HTTPS请求
  14. Pycharm 远程调试
  15. java中获取远程ip的一个坑
  16. 7F - 无限的路
  17. Oracle数据库over函数的使用
  18. macOS下appstore提示未能完成该操作的解决办法
  19. ansible的高级应用-roles
  20. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

热门文章

  1. hbase集群 常用维护命令
  2. 李洪强漫谈iOS开发[C语言-013]-常量
  3. android 内部存储相关知识点: getfilestreampath getDir 子文件夹
  4. png24是支持Alpha透明的。。。。。。
  5. *[topcoder]JumpFurther
  6. Qt: 界面中使用中文(三种方法,QApplication::translate可指定编码)
  7. Unable to resolve target 'android-8'类似错误的解决办法
  8. 安装Hadoop系列 — 导入Hadoop源码项目
  9. hdr_beg(host) hdr_reg(host) hdr_dom(host)
  10. Java 简单的加密解密算法