Sobes.tech
Junior — Middle

What happens in a Go program if you try to read from a closed channel?

sobes.tech AI

Answer from AI

If you try to read from a closed channel in Go, the read operation will return the zero value of the channel type and false as the second return value if using the two-value syntax:

v, ok := <-ch

Here, ok will be false, indicating that the channel is closed and no more data will be received.

If you read from a closed channel without checking ok, you will get the zero value of the channel type (for example, 0 for int).

Important: reading from a closed channel does not block and does not cause a panic; it immediately returns the zero value and false.

What happens in a Go program if you try to read from… - sobes.tech