× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Concurrent send/receive go channel

Martin Czygan
May 05, 2015
<p>As <a href="http://stackoverflow.com/a/29719829/89391">@Tim</a> said, you only need a single <code>for</code>, since <code>range</code> will emit values from the channel, until it is closed.</p> <p>Overall, the pattern you describe is called <a href="https://blog.golang.org/pipelines" rel="nofollow">fan-in</a>. A example for a basic producer/consumer setup can be found here: <a href="http://play.golang.org/p/AhQ012Qpwj" rel="nofollow">http://play.golang.org/p/AhQ012Qpwj</a>. The range loop runs in the consumer:</p> <pre><code>// consumer acts as fan in, signals when it is done. func consumer(out chan string, done chan bool) { for value := range out { fmt.Println(value) } done &lt;- true } </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/29719780/Concurrent%20send/receive%20go%20channel/29719960">Stack Overflow</a>.</p>
comments powered by Disqus