转自老外的文章:http://tleyden.github.io/blog/2013/11/23/understanding-chan-chans-in-go/

A channel describes a transport of sorts. You can send a thing down that transport. When using a chan chan, the thing you want to send down the transport is another transport to send things back.

They are useful when you want to get a response to something, and you don’t want to setup two channels (it’s generally considered bad practice to have data moving bidirectionally on a single channel)

Visual time lapse walkthrough

Keep in mind that Goroutine C is the “real consumer” even though it will be the one which writes to the request channel.

The request channel starts out empty.

Goroutine C passes a “response channel” to go routine D via the request channel

Goroutine C starts reading from the (still empty) response channel.

Goroutine D writes a string to the response channel

Goroutine C now is able to read a value from response channel, and get’s the “wassup!” message

And now we are back to where we started

Here is some code that uses chan chan’s

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  1. package main
  2. import "fmt"
  3. import "time"
  4. func main() {
  5. // make the request chan chan that both go-routines will be given
  6. requestChan := make(chan chan string)
  7. // start the goroutines
  8. go goroutineC(requestChan)
  9. go goroutineD(requestChan)
  10. // sleep for a second to let the goroutines complete
  11. time.Sleep(time.Second)
  12. }
  13. func goroutineC(requestChan chan chan string) {
  14. // make a new response chan
  15. responseChan := make(chan string)
  16. // send the responseChan to goRoutineD
  17. requestChan <- responseChan
  18. // read the response
  19. response := <-responseChan
  20. fmt.Printf("Response: %v\n", response)
  21. }
  22. func goroutineD(requestChan chan chan string) {
  23. // read the responseChan from the requestChan
  24. responseChan := <-requestChan
  25. // send a value down the responseChan
  26. responseChan <- "wassup!"
  27. }

This code can be run on Go playground

Understanding Chan Chan's in Go的更多相关文章

  1. golang使用chan注意事项

    背景 最近老代码中遇到的一个问题,表现为: goroutine数量在高峰期上涨,上涨后平峰期将不下来.也就是goroutine泄露 使用pprof看,进程堵塞在chan chan的使用经验 在使用ch ...

  2. Golang: chan定义问题(7)

    通常都是定义读写双向的 chan,定义单向 chan 问题. 专栏的介绍可以参考 <GotchaGolang专栏>,代码可以看<宝库-Gotcha>. 通过 只写 chan 传 ...

  3. golang 裸写一个pool池控制协程的大小

    这几天深入的研究了一下golang 的协程,读了一个好文 http://mp.weixin.qq.com/s?__biz=MjM5OTcxMzE0MQ==&mid=2653369770& ...

  4. python远程连接paramiko 模块和堡垒机实现

    paramiko使用 paramiko模块是基于python实现了SSH2远程安全连接,支持认证和密钥方式,可以实现远程连接.命令执行.文件传输.中间SSH代理功能 安装 pip install pa ...

  5. python之实现基于paramiko和mysql数据库的堡垒机

    一.堡垒机结构 堡垒机执行流程: 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码) 用户登陆堡垒机,输入堡垒机用户名密码,现实当前用户管理的服务器列表 用户选择服务器,并自动登陆 ...

  6. 简单的验证码识别(opecv)

    opencv版本: 3.0.0 处理验证码: 纯数字验证码 (颜色不同,有噪音,和带有较多的划痕) 测试时间 :  一天+一晚 效果: 比较挫,可能是由于测试的图片是在太小了的缘故. 原理:  验证码 ...

  7. Go语言实现简单的一个静态WEB服务器

    package main import ( "net/http" ) func main() { http.Handle("/", http.FileServe ...

  8. SQLALchemy(连表)、paramiko

    本节内容:

  9. 二:Go编程语言规范-类型

    1.类型 布尔值,数值与字符串类型的实例的命名是预声明的. 数组,结构,指针,函数,接口,切片,映射和信道这些复合类型可由类型字面构造. 每个类型 T 都有一个 基本类型:若 T 为预声明类型或类型字 ...

随机推荐

  1. Go! new Hello World, 我的第一个Go程序

    以下语句摘自百度百科: Go语言是谷歌2009发布的第二款开源编程语言. Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进 ...

  2. 计算机开放电子书汇总(包括二十多本python相关的图书教程)

    计算机开放电子书汇总(包括二十多本python相关的图书教程) https://github.com/it-ebooks/it-ebooks-archive 这个汇总包含了各种计算机相关的开放图书和文 ...

  3. 还有一个月,或者不到一个月就要期末了,复习ing

    首先,线性代数,课程设计,php  ,数据库,操作系统,还有概率论 ,四级

  4. 一些Layout的坑。坑死我自己了

    iOS这个东西,初学感觉,还好还好,然后一年之后再来修复一下初学的时候的代码,我只是感觉头很晕- - 别扶我. AutoLayout的坑,明明以前都没有的!!!升了iOS10就突然发现了这个坑,其实也 ...

  5. 互联网实习笔记之shell笔记

    linux下面一切都是可以配置的 #vim可以有 .vimrc文件 #------.vimrc开始---- set vb t_vb= set number syntax on set hlsearch ...

  6. 虚机centos和本机Windows之间文件的拷贝无法用xftp时用FileZilla也行

    步骤如下: 1.如果Centos没有安装ssh,则需要先安装: 2.查看虚拟机中IP地址,命令如下: ifconfig 3.在windows中安装ftp软件 FileZilla启动软件如图: 6  这 ...

  7. Android 标题栏菜单设置与应用(popupWindow的应用)

    效果图

  8. JavaScript获取一段html片段中a标签的href值

    最近,做项目中有一个需求,页面中有一个文本编辑器,里面写的内容最后生成了html代码片段,在另一个页面需要前一个页面文本编辑器的html代码片段中的a标签的href值,就尝试做了,因为不太熟悉js,所 ...

  9. NSOperationQueue的基本使用

    NSOperationQueue的作用 NSOperation可以调用start方法来执行任务,但默认是同步执行的 如果将NSOperation添加到NSOperationQueue(操作队列)中,系 ...

  10. 探索软件工程道路上的我III (Θ∀Θ#)

    github地址:https://github.com/JUNYU217/2016-03-08 开发语言:Java 开发工具:UltraEdit || 为月末了,网费欠了...很抱歉的拖了那么久的作业 ...