Sobes.tech
Junior — Middle

Mis juhtub, kui teete kirjutamisoperatsiooni tühja (nil) kanali puhul Go keeles?

sobes.tech AI

Vastus AI-lt

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

Example:

var ch chan int // ch == nil

func main() {
    ch <- 1 // here the program will hang
}

To avoid this, the channel should be initialized with make, for example:

ch := make(chan int)

Only after this, it is safe to write to and read from the channel.