Add second return value to read example

Also revert too complicated paragraph
This commit is contained in:
asborisjuas 2023-11-30 10:43:42 +03:00
parent 7a8c91630d
commit 794330689b
4 changed files with 23 additions and 25 deletions

View File

@ -48,9 +48,11 @@ func main() {
// 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
// It is possible to read more from an empty closed channel.
// However, instead of waiting for a message, we will always
// immediately receive a zero value of the channel's type and
// a false bool flag indicating that we should stop reading from it.
j, isOpened := <-jobs
fmt.Println("no jobs to receive", j)
fmt.Println("awaiting more jobs:", isOpened)
}

View File

@ -1,2 +1,2 @@
56d8d399e304cdf5ab36663b43397ca43126e260
cNvXGNi9l27
3b474131d4d983ac5e53d8a6b94e069a8a4b775d
yLh6yhTGZeF

View File

@ -8,10 +8,7 @@ received job 3
sent all jobs
received all jobs
no jobs to receive 0
awaiting more jobs: false
# 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.
# The idea of closed channels leads naturally to our next
# example: `range` over channels.

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/cNvXGNi9l27"><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/yLh6yhTGZeF"><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>
@ -134,15 +134,17 @@ we saw earlier.</p>
<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>
<p>It is possible to read more from an empty closed channel.
However, instead of waiting for a message, we will always
immediately receive a zero value of the channel&rsquo;s type and
a false bool flag indicating that we should stop reading from it.</p>
</td>
<td class="code">
<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>
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="nx">j</span><span class="p">,</span> <span class="nx">isOpened</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="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;awaiting more jobs:&#34;</span><span class="p">,</span> <span class="nx">isOpened</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span></span></span></code></pre>
</td>
</tr>
@ -166,18 +168,15 @@ immediately return zero value of the channel&rsquo;s type.</p>
</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><span class="line"><span class="cl"><span class="go">no jobs to receive 0</span></span></span></code></pre>
</span></span></span><span class="line"><span class="cl"><span class="go">no jobs to receive 0
</span></span></span><span class="line"><span class="cl"><span class="go">awaiting more jobs: false</span></span></span></code></pre>
</td>
</tr>
<tr>
<td class="docs">
<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>
<p>The idea of closed channels leads naturally to our next
example: <code>range</code> over channels.</p>
</td>
<td class="code empty">
@ -201,7 +200,7 @@ see our next example.</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');codeLines.push(' j :\u003D \u003C-jobs\u000A fmt.Println(\"no jobs to receive\", j)\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, isOpened :\u003D \u003C-jobs\u000A fmt.Println(\"no jobs to receive\", j)\u000A fmt.Println(\"awaiting more jobs:\", isOpened)\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>