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的更多相关文章

  1. A Tour of Go Buffered Channels

    Channels can be buffered. Provide the buffer length as the second argument to make to initialize a b ...

  2. 后端程序员之路 53、A Tour of Go-3

    #method    - Methods        - Go does not have classes. However, you can define methods on types.    ...

  3. POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9276   Accepted: 3924 ...

  4. Euler Tour Tree与dynamic connectivity

    Euler Tour Tree最大的优点就是可以方便的维护子树信息,这点LCT是做不到的.为什么要维护子树信息呢..?我们可以用来做fully dynamic connectivity(online) ...

  5. POJ2677 Tour[DP 状态规定]

    Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4307   Accepted: 1894 Description ...

  6. Django Channels 学习笔记

    一.为什么要使用Channels 在Django中,默认使用的是HTTP通信,不过这种通信方式有个很大的缺陷,就是不能很好的支持实时通信.如果硬是要使用HTTP做实时通信的话只能在客户端进行轮询了,不 ...

  7. 【Go入门教程7】并发(goroutine,channels,Buffered Channels,Range和Close,Select,超时,runtime goroutine)

    有人把Go比作21世纪的C语言,第一是因为Go语言设计简单,第二,21世纪最重要的就是并行程序设计,而Go从语言层面就支持了并行. goroutine goroutine是Go并行设计的核心.goro ...

  8. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

  9. JAVA NIO中的Channels和Buffers

    前言 Channels和Buffers是JAVA NIO里面比较重要的两个概念,NIO正是基于Channels和Buffers进行数据操作,且数据总是从Channels读取到Buffers,或者从Bu ...

随机推荐

  1. 一个批量转换jtl文件的shell

    最近在项目中遇到了批量转换jmeter测试结果jtl的问题,整了一个脚本,记录如下: #bin/sh filelist=`ls jtl` # 将jtl目录的所有文件列表读取并存入变量 for file ...

  2. 126. Word Ladder II

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  3. Android之AlertDialog.Builder详解

    import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; ...

  4. “段寄存器”的故事[转](彻底搞清内存段/elf段/实模式保护模式以及段寄存器)

    http://blog.csdn.net/michael2012zhao/article/details/5554023 一. 段寄存器的产生 段寄存器的产生源于Intel 8086 CPU体系结构中 ...

  5. 2013ACM省赛题目

    地址就贴这一个吧 都在附近 当时回来也没做做 一伤心了 二是当时实在太弱了 先补两道DP E题的区间DP dp[i][j]  截止到i位置以字母j为结束的上升序列 正序 逆序各来一遍 再循环一遍保存一 ...

  6. shell脚本编写笔记

    包含转载内容,转载自http://wenku.baidu.com/link?url=jtCHxEYzgGve6P64U3JRQRgU6nhpGvqFLLpWu9I2Htq6hi9TGLudRFkk7r ...

  7. 理解$watch ,$apply 和 $digest --- 理解数据绑定过程

    原文地址:http://angular-tips.com/blog/2013/08/watch-how-the-apply-runs-a-digest/ 注 这篇博文主要是写给新手的,是给那些刚刚开始 ...

  8. LA 6540 Fibonacci Tree

    以前做过的题···重新做一遍之后怎么做怎么wa···后来GG了···第二天看不知道为啥A了···难道我失忆了? 题意:无向图,边有黑色和白色两种颜色,求是否存在一个生成树中白边的个数是斐波那契数. 解 ...

  9. MFC 状态栏相关使用(CStatusBar & CStatusBarCtrl)

    原文:MFC 状态栏相关使用(CStatusBar & CStatusBarCtrl),沙漠紫风铃 本文介绍了MFC中和状态栏相关的用法: 在MFC的的单文档应用中,在建好应用程序之后,CMa ...

  10. 关于SQL中的Update语句

    今天在SQL数据库操作时需要将一张表中的数据Update到另一张表中去, 可是用我以往的写法确怎么也不能成功.代码如下: update table1 a set a.Col1=b.Col2 from ...