A Tour of Go Channels
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)
}
A Tour of Go Channels的更多相关文章
- A Tour of Go Buffered Channels
Channels can be buffered. Provide the buffer length as the second argument to make to initialize a b ...
- 后端程序员之路 53、A Tour of Go-3
#method - Methods - Go does not have classes. However, you can define methods on types. ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- Euler Tour Tree与dynamic connectivity
Euler Tour Tree最大的优点就是可以方便的维护子树信息,这点LCT是做不到的.为什么要维护子树信息呢..?我们可以用来做fully dynamic connectivity(online) ...
- POJ2677 Tour[DP 状态规定]
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4307 Accepted: 1894 Description ...
- Django Channels 学习笔记
一.为什么要使用Channels 在Django中,默认使用的是HTTP通信,不过这种通信方式有个很大的缺陷,就是不能很好的支持实时通信.如果硬是要使用HTTP做实时通信的话只能在客户端进行轮询了,不 ...
- 【Go入门教程7】并发(goroutine,channels,Buffered Channels,Range和Close,Select,超时,runtime goroutine)
有人把Go比作21世纪的C语言,第一是因为Go语言设计简单,第二,21世纪最重要的就是并行程序设计,而Go从语言层面就支持了并行. goroutine goroutine是Go并行设计的核心.goro ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
- JAVA NIO中的Channels和Buffers
前言 Channels和Buffers是JAVA NIO里面比较重要的两个概念,NIO正是基于Channels和Buffers进行数据操作,且数据总是从Channels读取到Buffers,或者从Bu ...
随机推荐
- 使用QGridLayout布局实现翻页效果
http://blog.csdn.net/u013704336/article/details/51474942
- Revit 二次开发 沿弧形路径创建拉伸屋顶
沿弧形路径创建拉伸屋顶 Revit的API中只能按照直线创建拉伸屋顶,不能按照曲线创建拉伸屋顶.在Revit的界面当中,可以用 构建->内建模型,进行放样创建屋顶.但是没有办法代码内建模型. 可 ...
- GCC警告选项例解
程序员是追求完美的一族,即使是一般的程序员大多也都不想看到自己的程序中有甚至那么一点点的瑕疵.遇到任意一条编译器警告都坚决不放过.有人会说:我们可以使用比编译器更加严格的静态代码检查工具,如splin ...
- Glimpse
给自己程序配好Glimpse. Glimpse.Mvc 有问题 遇到稍微复杂点的内套多个PartialView,内存就爆了彪1.7g,不开Glimpse一点问题都没.另外Glimpse.Nlog也有问 ...
- bzoj1433:[ZJOI2009]假期的宿舍
明显的二分图最大匹配. #include<cstdio> #include<cstring> #include<cctype> #include<algori ...
- wpa_supplicant是什么?
/************************************************************************ * wpa_supplicant是什么? * 声明: ...
- 【Java】【编译】javac编译源代码时,若源文件使用了别的java源代码的函数,javac会自动关联。
* 算法第四版自己的alg4.jar似乎有些过时. * 可以引用别的源码文件里的函数
- 解决java switch……case不能匹配字符串的问题
java1.7已经支持了匹配字符串 方案1. enum Animal { dog,cat,bear; public static Animal getAnimal(String animal){ re ...
- 表格的一些原生js操作(隔行变色,高亮显示,添加删除,搜索)
看着网上的视频教程,虽说还是有点简单,但还是不免想记录下.这些操作包括(隔行变色,高亮显示,添加删除,搜索功能),而这儿就是涉及table的原有属性“tBodies” “rows” “cells”等几 ...
- [转]ubuntu zip 文件乱码解决 压缩乱码
ubuntu zip 文件乱码解决 压缩乱码 1.1 通过unzip行命令解压,指定字符集 unzip -O CP936 xxx.zip (用GBK, GB18030也可以) 有趣的是unzip的ma ...