Skip to content

Instantly share code, notes, and snippets.

View cpusoft's full-sized avatar

cpusoft cpusoft

  • Beijing
View GitHub Profile
@cpusoft
cpusoft / channel.md
Created November 29, 2019 06:42 — forked from xin053/channel.md
[go channel] go channel #go #channel

channel 重点

Operation A Nil Channel A Closed Channel A Not-Closed Non-Nil Channel
Close panic panic succeed to close (C)
Send Value To block for ever panic block or succeed to send (B)
Receive Value From block for ever never block (D) block or succeed to receive (A)

一个基于无缓存 channel 的发送操作将导致发送者 goroutine 阻塞, 直到另一个 goroutine 在相同的 channel 上执行接收操作, 当发送的值通过 channel 成功传输之后, 两个 goroutine 可以继续执行后面的语句, 反之,如果接收操作先发生, 那么接收者 goroutine 也将阻塞, 直到有另一个 goroutine 在相同的 channel 上执行发送操作