Range and Close
A sender can close
a channel to indicate that no more values will be sent. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression:
ok
is false
if there are no more values to receive and the channel is closed.
The loop for i := range c
receives values from the channel repeatedly until it is closed. Channels aren't like files, so they don't usually need to closed. Closing is only necessary when the receiver must be told there are no more values coming, such terminating the range
loop.
Example
Last updated