Sobes.tech
Senior

Mis juhtub, kirjutades mitte-initsialiseeritud (nil) kanalile?

sobes.tech AI

Vastus AI-lt

In Go, writing to an uninitialized (nil) channel causes the goroutine to block forever, because a nil channel is not ready to receive or send data.

Example:

var ch chan int // ch == nil

func main() {
    ch <- 1 // blocks forever, as the channel is nil
}

This behavior is sometimes used to dynamically enable/disable channels in select, since operations with nil channels never execute and block the goroutine.