Sobes.tech
Junior — Middle

Kas saab andmeid saata kanalile, mille väärtus on nil?

sobes.tech AI

Vastus AI-lt

In Go, it is not possible to 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, because such a channel is not initialized and cannot receive data.

Example:

var ch chan int // ch == nil

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

// program will hang

To avoid blocking, initialize the channel before use:

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

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