Junior — Middle
How does writing to a channel that has not been initialized yet work?
sobes.tech AI
Answer from AI
In Go, a channel must be initialized using the make function before use. If you try to send to a channel that has not been initialized (i.e., is nil), the send operation will block forever because the channel does not exist and cannot accept a value. This will lead to a deadlock if there are no other goroutines that unblock the send.
Example:
var ch chan int // ch == nil
func main() {
ch <- 1 // blocks because ch == nil
}
Thus, sending to an uninitialized channel does not work and causes the program to block.