Sobes.tech
Junior — Middle

Is it possible to send data to a channel that has a nil value?

sobes.tech AI

Answer from AI

In Go, you cannot send data to a channel if it is nil. Attempting to send a value to a nil channel will cause the goroutine to block forever, as such a channel is uninitialized and cannot receive data.

Example:

var ch chan int // ch == nil

go func() {
    ch <- 1 // blocks forever
}()

// program will hang

To avoid blocking, you should initialize the channel before use:

ch := make(chan int)
ch <- 1 // works correctly

If the channel can be nil, you should check its value before sending or use select with default to prevent blocking.

Is it possible to send data to a channel that has a… - sobes.tech