Go OR-Done-Channel Pattern
Jump to navigation
Jump to search
Internal
Overview
Reading from a channel until the channel is closed can be done by ranging over a channel:
for v := range c {
// do something with the channel value, we exit the loop automatically when the channel is closed
...
}
The for
/range
syntax is simple and intuitive, but it cannot be used anymore if we want to make the loop preemptable by using a done
channel. In that case we need to replace it with a for
/select
syntax and case
for the state of the done
channel.