Go OR-Done-Channel Pattern: Difference between revisions
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This is a simple and intuitive syntax, but | This is a simple and intuitive syntax, but <code>for</code>/<code>range</code> syntax cannot be used anymore if we want to [[Go_Channels#Use_a_done_Channel|make the loop preemptable by using a <code> channel]]. In that case we need to use a <code>for</code>/<code>select</code> syntax and <code>case</code> for the state of the <code>done</code> channel. |
Revision as of 22:18, 5 February 2024
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
...
}
This is a simple and intuitive syntax, but for
/range
syntax cannot be used anymore if we want to make the loop preemptable by using a channel
. In that case we need to use a
for
/select
syntax and case
for the state of the done
channel.