码迷,mamicode.com
首页 > 其他好文 > 详细

Go Concurrency

时间:2015-02-06 20:29:40      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

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

Go Concurrency

标签:

原文地址:http://www.cnblogs.com/tekkaman/p/4277939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!