Sobes.tech
Junior — Middle

What happens if you try to read data from a channel that has not been initialized yet?

sobes.tech AI

Answer from AI

If you try to read data from a channel in Go that has not been initialized (i.e., is nil), the read operation will block forever. This will cause the goroutine attempting to read from such a channel to hang, as the channel does not exist and cannot transmit data.

Example:

var ch chan int // ch is nil

func main() {
    fmt.Println(<-ch) // blocks forever
}

To avoid this, the channel should be initialized using make, for example: ch := make(chan int).

What happens if you try to read data from a channel… - sobes.tech