Go Concurrency

tekkaman · · 2324 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Go Concurrency

1、A goroutine is a lightweight thread managed by the Go runtime.

  

2、Channels are a typed conduit through which you can send and receive values with the channel operator, <-.

  

3、Like maps and slices, channels must be created before use:

  

  By default, sends and receives block until the other side is ready. This allows goroutines to synchronize without explicit locks or condition variables.

  

4、Channel会缓存。

  

5、A sender can close a channel to indicate that no more values will be sent. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression: after

  

  ok is false if there are no more values to receive and the channel is closed.

  The loop for i := range c receives values from the channel repeatedly until it is closed.

  Note: Only the sender should close a channel, never the receiver. Sending on a closed channel will cause a panic.

  Another note: Channels aren't like files; you don't usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a range loop.

  

6、The select statement lets a goroutine wait on multiple communication operations.

  A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.

  

  

7、The default case in a select is run if no other case is ready.

  Use a default case to try a send or receive without blocking:

  

参考:https://tour.golang.org/concurrency/1


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:tekkaman

查看原文:Go Concurrency

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2324 次点击  
加入收藏 微博
3 回复  |  直到 2017-08-26 12:41:12
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传