This is a creation in Article, where the information may have evolved or changed.
(go from Go technology morning reading)
The Go language channel has a seemingly odd feature, which is that if you write to or read data to a channel that is null (nil), the current goroutine will block forever.
The above four main functions will always block (but because there is no other goroutine, runtime will report a deadlock error)
Why did go team design this?
It is said to implement a pattern called "guarded selective wating", and conditional wait: In some cases in select, if the corresponding conditions are not met, wait on this case.
There is such a scenario:
With the nil channel feature, it can be implemented with great elegance:
These are permanent blocks that are implemented using nil channel.
——-——————————— Ornate split line ——————————————
If we create a channel, but do not write data to it is not closed, just read the data, can also achieve the effect of permanent blocking. The effect is as follows:
This means that the permanent blocking feature of the nil channel is not necessary for the implementation of the "guarded selective wating" mode, but the nil channel is obviously more convenient, There is no need to waste additional resources to create a channel to permanently block.