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 ...
随机推荐
- HDU1437+模拟
枚举中间可能出现的天气 #include<stdio.h> #include<string.h> #include<stdlib.h> ; ][ ]; void s ...
- FocusWriter
2. FocusWriter 如果你正在从事某种写作——小说.博客.文档等——你绝对希望认识一下FocusWriter.它已经有近十年的发布历史了,但是一直是我们最喜欢的无分心写作应用之一.如果你希望 ...
- Oracl 动态执行表不可访问,本会话的自动统计被禁止
oracle ---建立SQL窗体 写入 select * from tableA; 弹出错误窗口 : 动态执行表不可访问,本会话的自动统计被禁止.在执行菜单里你可以禁止统计,或在v$session, ...
- POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)
The Embarrassed Cryptographer DescriptionThe young and very promising cryptographer Odd Even has imp ...
- 【原创】oracle的tpc-c测试及方法
大家好,很高兴来到博客园分享自己的所见所得.希望和大家多多交流,共同进步. 本文重点在于简介使用BenchmarkSQL对oracle进行tpcc的测试步骤,只是一个简单入门的过程. 开源测试工具:B ...
- 【HDOJ】3686 Traffic Real Time Query System
这题做了几个小时,基本思路肯定是求两点路径中的割点数目,思路是tarjan缩点,然后以割点和连通块作为新节点见图.转化为lca求解.结合点——双连通分量与LCA. /* 3686 */ #includ ...
- 推荐:ThoughtWorks(中国)程序员读书雷达
部分转自张逸的博客:http://agiledon.github.io/blog/2013/04/17/thoughtworks-developer-reading-radar/ 长久以来一直对程序员 ...
- bzoj1532
就题目而言,这道题是裸的二分+最大流 但是这样是TLE的,我们考虑优化 1. 我们可以先贪心,这样二分的上界就可以缩小了 2. 最大流我们可以不急着跑增广路,我们可以先贪心一个流然后再跑增广路 但是我 ...
- bzoj1821: [JSOI2010]Group 部落划分 Group
kruskal算法. #include<cstdio> #include<algorithm> #include<cstring> #include<cmat ...
- [原]Unity3D深入浅出 - 物理材质(Physics Materials)
在Unity3d中已经配置好了5种常用的物理材质,Bouncy.Ice.Metal.Rubber.Wood,在菜单中依次选择Assets - Import Package - Physics Mate ...