Golang中 channel 以及 groutine 理解

1、Curious Channels

A closed channel never blocks
A nil channel always blocks

A send to a nil channel blocks forever
A receive from a nil channel blocks forever
A send to a closed channel panics
A receive from a closed channel returns the zero value immediately

2.Never start a goroutine without knowing how it will stop

Every time you write the statement go in a program, you should consider the question of how, and under what conditions, the goroutine you are about to start, will end.

select会按照随机的顺序检测各case语句中channel是否ready,如果某个case中的channel已经ready则执行相应的case语句然后退出select流程,如果所有的channel都未ready且没有default的话,则会阻塞等待各个channel

引用:
https://dave.cheney.net/2016/...

https://dave.cheney.net/2013/...

https://dave.cheney.net/2014/...

The Behavior Of Channels:
https://www.ardanlabs.com/blo...

你可能感兴趣的