Add example of reading from closed channel

This commit is contained in:
asborisjuas 2023-11-28 18:40:57 +03:00
parent 294387674a
commit 7a8c91630d
4 changed files with 40 additions and 10 deletions

View File

@ -47,4 +47,10 @@ func main() {
// [synchronization](channel-synchronization) approach
// we saw earlier.
<-done
// It is possible to read from an empty closed channel,
// but instead of waiting for a message, we will
// immediately receive a zero value of the channel's type.
j := <-jobs
fmt.Println("no jobs to receive", j)
}

View File

@ -1,2 +1,2 @@
8f26c901e0f14df2ca40329a354c3ac86a5c3a07
vCvRjcMq7p3
56d8d399e304cdf5ab36663b43397ca43126e260
cNvXGNi9l27

View File

@ -7,6 +7,11 @@ sent job 3
received job 3
sent all jobs
received all jobs
no jobs to receive 0
# The idea of closed channels leads naturally to our next
# example: `range` over channels.
# Be sure that channel is not closed when you read
# from it, especially when iterating over a channel.
# Otherwise you might get an unexpected result or
# even enter an infinite loop.
# To learn how to correctly use `range` over channels,
# see our next example.

View File

@ -43,7 +43,7 @@ completion to the channel&rsquo;s receivers.</p>
</td>
<td class="code leading">
<a href="https://go.dev/play/p/vCvRjcMq7p3"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<a href="https://go.dev/play/p/cNvXGNi9l27"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><code><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span></span></span></code></pre>
</td>
</tr>
@ -125,10 +125,24 @@ channel, then closes it.</p>
<a href="channel-synchronization">synchronization</a> approach
we saw earlier.</p>
</td>
<td class="code leading">
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="o">&lt;-</span><span class="nx">done</span></span></span></code></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>It is possible to read from an empty closed channel,
but instead of waiting for a message, you will
immediately return zero value of the channel&rsquo;s type.</p>
</td>
<td class="code">
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="o">&lt;-</span><span class="nx">done</span>
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="nx">j</span> <span class="o">:=</span> <span class="o">&lt;-</span><span class="nx">jobs</span>
</span></span><span class="line"><span class="cl"> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;no jobs to receive&#34;</span><span class="p">,</span> <span class="nx">j</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre>
</td>
</tr>
@ -151,14 +165,19 @@ we saw earlier.</p>
</span></span></span><span class="line"><span class="cl"><span class="go">sent job 3
</span></span></span><span class="line"><span class="cl"><span class="go">received job 3
</span></span></span><span class="line"><span class="cl"><span class="go">sent all jobs
</span></span></span><span class="line"><span class="cl"><span class="go">received all jobs</span></span></span></code></pre>
</span></span></span><span class="line"><span class="cl"><span class="go">received all jobs
</span></span></span><span class="line"><span class="cl"><span class="go">no jobs to receive 0</span></span></span></code></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>The idea of closed channels leads naturally to our next
example: <code>range</code> over channels.</p>
<p>Be sure that channel is not closed when you read
from it, especially when iterating over a channel.
Otherwise you might get an incorrect result or
even enter an infinite loop.
To learn how to correctly use <code>range</code> over channels,
see our next example.</p>
</td>
<td class="code empty">
@ -182,7 +201,7 @@ example: <code>range</code> over channels.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A jobs :\u003D make(chan int, 5)\u000A done :\u003D make(chan bool)\u000A');codeLines.push(' go func() {\u000A for {\u000A j, more :\u003D \u003C-jobs\u000A if more {\u000A fmt.Println(\"received job\", j)\u000A } else {\u000A fmt.Println(\"received all jobs\")\u000A done \u003C- true\u000A return\u000A }\u000A }\u000A }()\u000A');codeLines.push(' for j :\u003D 1; j \u003C\u003D 3; j++ {\u000A jobs \u003C- j\u000A fmt.Println(\"sent job\", j)\u000A }\u000A close(jobs)\u000A fmt.Println(\"sent all jobs\")\u000A');codeLines.push(' \u003C-done\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A jobs :\u003D make(chan int, 5)\u000A done :\u003D make(chan bool)\u000A');codeLines.push(' go func() {\u000A for {\u000A j, more :\u003D \u003C-jobs\u000A if more {\u000A fmt.Println(\"received job\", j)\u000A } else {\u000A fmt.Println(\"received all jobs\")\u000A done \u003C- true\u000A return\u000A }\u000A }\u000A }()\u000A');codeLines.push(' for j :\u003D 1; j \u003C\u003D 3; j++ {\u000A jobs \u003C- j\u000A fmt.Println(\"sent job\", j)\u000A }\u000A close(jobs)\u000A fmt.Println(\"sent all jobs\")\u000A');codeLines.push(' \u003C-done\u000A');codeLines.push(' j :\u003D \u003C-jobs\u000A fmt.Println(\"no jobs to receive\", j)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>