Closing a channel indicates that no more values will be sent on it. This can be useful to communicate completion to the channel’s receivers. |
|
![]() ![]()
|
|
|
|
In this example we’ll use a |
|
Here’s the worker goroutine. It repeatedly receives
from |
|
This sends 3 jobs to the worker over the |
|
We await the worker using the synchronization approach we saw earlier. |
|
It is possible to read from an empty closed channel, but instead of waiting for a message, you will immediately return zero value of the channel’s type. |
|
|
|
Be sure that channel is not closed when you read
from it, especially when iterating over a channel.
Otherwise you might get an incorrect result or
even enter an infinite loop.
To learn how to correctly use |
Next example: Range over Channels.