Golang Channel 实战技巧和说明
Channel 的一些实战说明
关于 close Channel
close Channel 的一些说明
channel 不需要通过 close 来释放资源,这个是它与 socket、file 等不一样的地方,对于 channel 而言,唯一需要 close 的就是我们想通过 close 触发 channel 读事件。
- close chan 对 chan 阻塞无效,写了数据不读,直接 close,还是会阻塞的。
- 如果 channel 已经被关闭,继续往它发送数据会导致 panic
send on closed channel
- closed 的 channel,再次关闭 close 会 panic
- close channel 的推荐使用姿势是在发送方来执行,因为 channel 的关闭在接收端能感知到,但是发送端感知不到,因此一般只能在发送端主动关闭。而且大部分时候可