Junior — Middle
What value does a closed channel return in the programming language?
sobes.tech AI
Answer from AI
In Go language, when reading from a closed channel, the zero value of the channel's type and the value false are returned as the second return value if the syntax with two return values is used. This allows you to determine that the channel is closed and will no longer send data.
Example:
ch := make(chan int)
close(ch)
v, ok := <-ch
fmt.Println(v, ok) // Will output: 0 false
Here, v is the zero value of type int (which is 0), and ok is false, indicating that the channel is closed.