From 794330689b671bfd6cc935c05f70498db55fcf23 Mon Sep 17 00:00:00 2001 From: asborisjuas Date: Thu, 30 Nov 2023 10:43:42 +0300 Subject: [PATCH] Add second return value to read example Also revert too complicated paragraph --- examples/closing-channels/closing-channels.go | 10 +++++--- .../closing-channels/closing-channels.hash | 4 +-- examples/closing-channels/closing-channels.sh | 9 +++---- public/closing-channels | 25 +++++++++---------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/examples/closing-channels/closing-channels.go b/examples/closing-channels/closing-channels.go index b00fff7..edc86d8 100644 --- a/examples/closing-channels/closing-channels.go +++ b/examples/closing-channels/closing-channels.go @@ -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) } diff --git a/examples/closing-channels/closing-channels.hash b/examples/closing-channels/closing-channels.hash index 1079571..41fbcca 100644 --- a/examples/closing-channels/closing-channels.hash +++ b/examples/closing-channels/closing-channels.hash @@ -1,2 +1,2 @@ -56d8d399e304cdf5ab36663b43397ca43126e260 -cNvXGNi9l27 +3b474131d4d983ac5e53d8a6b94e069a8a4b775d +yLh6yhTGZeF diff --git a/examples/closing-channels/closing-channels.sh b/examples/closing-channels/closing-channels.sh index 99cb663..1958328 100644 --- a/examples/closing-channels/closing-channels.sh +++ b/examples/closing-channels/closing-channels.sh @@ -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. diff --git a/public/closing-channels b/public/closing-channels index 186dcb3..4f98cbf 100644 --- a/public/closing-channels +++ b/public/closing-channels @@ -43,7 +43,7 @@ completion to the channel’s receivers.

- +
package main
@@ -134,15 +134,17 @@ we saw earlier.

-

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’s type.

+

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 := <-jobs
+          
    j, isOpened := <-jobs
     fmt.Println("no jobs to receive", j)
+    fmt.Println("awaiting more jobs:", isOpened)
 }
@@ -166,18 +168,15 @@ immediately return zero value of the channel’s type.

received job 3 sent all jobs received all jobs -no jobs to receive 0
+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 incorrect 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.

@@ -201,7 +200,7 @@ see our next example.