Range and Close

This lesson will teach you a way to close a channel.

If you remember the lesson on channels, you are already familiar with this pattern.

In Go, we have a range function which lets us iterate over elements in different data structures. Using this function, we can range over the items we receive on a channel until it is closed. Also, note that only the sender, not the receiver, should close the channel when it feels that it has no more values to send.

Let’s have a look at an example:

Get hands-on with 1200+ tech skills courses.