Sobes.tech
Middle+

What will happen if you read from a closed channel?

sobes.tech AI

Answer from AI

When reading from a closed channel in Go, the read operation does not block and returns the zero value of the channel's type along with a boolean indicating that the channel is closed. This allows goroutines that read from the channel to terminate safely without worrying about blocking.

Example:

ch := make(chan int)
close(ch)

v, ok := <-ch // ok == false, v == 0 (zero value of int)

Thus, reading from a closed channel always completes successfully, returning the zero value and false as the second return value.

What will happen if you read from a closed channel… - sobes.tech