Senior
What happens when an uninitialized channel is closed?
sobes.tech AI
Answer from AI
In Go, closing an uninitialized (i.e., nil) channel results in a panic (runtime panic). This is because the close operation expects a valid channel, and a nil channel does not exist.
Example:
var ch chan int // nil channel
close(ch) // panic: close of nil channel
To avoid panic, it is recommended to check that the channel is not nil before closing it.
Closing a channel signals to receivers that no more values will be sent, but only initialized channels should be closed to prevent errors.