golang 的 channel 实现 生产者/消费者 模型
2016-02-24 22:57
633 查看
package main import ( "fmt" "math/rand" "time" ) func productor(channel chan<- string) { for { channel <- fmt.Sprintf("%v", rand.Float64()) time.Sleep(time.Second * time.Duration(1)) } } func customer(channel <-chan string) { for { message := <-channel // 此处会阻塞, 如果信道中没有数据的话 fmt.Println(message) } } func main() { channel := make(chan string, 5) // 定义带有5个缓冲区的信道(当然可以是其他数字) go productor(channel) // 将 productor 函数交给协程处理, 产生的结果传入信道中 customer(channel) // 主线程从信道中取数据 }
转自:http://blog.csdn.net/u010020066/article/details/50516957
相关文章推荐
- GO语言下载、安装、配置
- Golang(Go语言)读取文件基本用法
- Golang(Go语言)代码技巧之数组(array)和切片(slice)
- Golang(Go语言)代码技巧之字符串(string)
- Golang(Go语言)内置函数之copy用法
- SAE 环境 django 配置
- 设计模式-单例模式(Go语言描述)
- 把vim当做golang的IDE
- LeetCode Algorithms #21 <Merge Two Sorted Lists>
- Mongo性能检查
- LeetCode Algorithms #231 <Power of Two>
- GCM Google官方示例的简单介绍和使用
- Qualcomm官方QDN1月EDM:属于你的Dragon Board 410c
- DragonBoard 410c HDMI配置的艰辛路
- Go文件上传的例子
- golang beego 路由个人理解
- beego里面自定义配置文件
- 【CodeForces 616D】Longest k-Good Segment
- django项目部署到SAE上的个人博客
- GO语言(golang)官方网站!