In the previous sections, we have looked at three types of channels and how to create them. In this section, let's look at how to close the channels and how this might affect sending and receiving on these channels. We close a channel when we no longer want to send any messages on the said channel. How a channel behaves after being closed is different for each type of channel. Let's dive into them:
- Unbuffered closed channel: Sending messages will cause panic and receiving on it will yield an immediate zero value of the channel's element type.
- Buffered closed channel: Sending messages will cause panic but receiving on it will first yield all the values in the channel's queue. Once the queue has been exhausted, then the channel will start yielding zero values of the channel's element type.
The following is a program to elucidate on the...