转自老外的文章: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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main

import "fmt"
import "time" func main() { // make the request chan chan that both go-routines will be given
requestChan := make(chan chan string) // start the goroutines
go goroutineC(requestChan)
go goroutineD(requestChan) // sleep for a second to let the goroutines complete
time.Sleep(time.Second) } func goroutineC(requestChan chan chan string) { // make a new response chan
responseChan := make(chan string) // send the responseChan to goRoutineD
requestChan <- responseChan // read the response
response := <-responseChan fmt.Printf("Response: %v\n", response) } func goroutineD(requestChan chan chan string) { // read the responseChan from the requestChan
responseChan := <-requestChan // send a value down the responseChan
responseChan <- "wassup!" }

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. rails 常用的验证方法 validates (转)

    Agile Web Development with Rails 17.4 validation validate              在save的时候激活validate_on_create  ...

  2. jbox用法

    详见  http://www.jjsp.gov.cn:8888/js/jbox-v2.3/jbox-demo2.html

  3. 一步一步搭框架(asp.netmvc+easyui+sqlserver)-01

    一步一步搭框架(asp.netmvc+easyui+sqlserver)-01 要搭建的框架是企业级开发框架,适用用企业管理信息系统的开发,如:OA.HR等 1.框架名称:sampleFrame. 2 ...

  4. 初探NIOS II之hello_world

    平台背景: 操作系统:win7  64bit 开发板:DE2-115 Quartus ii:15.0及配套的NIOS ii开发平台 一.硬件系统的建立 1.在Quartus里新建工程,这是很基本的就不 ...

  5. tcp状态机

    tcp共有11种状态,其中涉及到关闭的状态有5 个.这5 个状态相互关联,相互纠缠,而且状态变化触发都是由应用触发,但是又涉及操作系统和网络,所以正确的理解TCP 在关闭时网络状态变化情况,为我们诊断 ...

  6. Linq学习总结2--Linq to XML

    概述: linq to xml(下面简称ltx好了),是微软根据linq技术对于XML的CURD.使用起来比System.XML中的XML操作方式更加简便.这段时间使用它在公司里升级了老板的邮件系统, ...

  7. RainCup_No.1

    Rain杯No.1 初见篇 本系列故事以及人名地名等纯属虚构,如有雷同,纯属巧合 在极东之地,有一个岛国,与岛国隔了一个海域有一个古老的国度,天朝.天朝T镇有个少年叫小S,故事从小S与少女Rain的相 ...

  8. google vr开源 cardboard

    https://developers.google.com/cardboard/android/ 待续

  9. css声明应用优先级

    1.特殊性 选择器的特殊值由四部分组成:0,0,0,0 第一位预留给内联样式   <h1 style="color:red;"></h1> 第二位属于ID选 ...

  10. Those who are not capable of Control their moods are not supposed to be ready for their baby.

    I hate these Stupid Selfish People. We need Children Caring Organization.