Sobes.tech
Junior — Middle

What happens if you perform a write operation on an empty (nil) channel in Go?

sobes.tech AI

Answer from AI

If you try to write a value into a nil channel in Go, the program will deadlock forever. This happens because a nil channel is uninitialized and cannot send or receive values.

Example:

var ch chan int // ch == nil

func main() {
    ch <- 1 // this will hang
}

To avoid this, initialize the channel with make, for example:

ch := make(chan int)

Only after that can you safely send and receive from the channel.