From 461f6955d69bb468b92d9c9abeec55a3befac123 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Thu, 30 Nov 2023 05:08:42 -0800 Subject: [PATCH] Fix up formatting and phrasing for closed chan read --- examples/closing-channels/closing-channels.go | 16 +++++++------ .../closing-channels/closing-channels.hash | 4 ++-- examples/closing-channels/closing-channels.sh | 3 +-- public/closing-channels | 23 ++++++++++--------- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/examples/closing-channels/closing-channels.go b/examples/closing-channels/closing-channels.go index edc86d8..9abc843 100644 --- a/examples/closing-channels/closing-channels.go +++ b/examples/closing-channels/closing-channels.go @@ -48,11 +48,13 @@ func main() { // we saw earlier. <-done - // 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) + // Reading from a closed channel succeeds immediately, + // returning the zero value of the underlying type. + // The optional second return value is `true` if the + // value received was delivered by a successful send + // operation to the channel, or `false` if it was a + // zero value generated because the channel is closed + // and empty. + _, ok := <-jobs + fmt.Println("received more jobs:", ok) } diff --git a/examples/closing-channels/closing-channels.hash b/examples/closing-channels/closing-channels.hash index 41fbcca..ecbc461 100644 --- a/examples/closing-channels/closing-channels.hash +++ b/examples/closing-channels/closing-channels.hash @@ -1,2 +1,2 @@ -3b474131d4d983ac5e53d8a6b94e069a8a4b775d -yLh6yhTGZeF +13f0ccf3674db8e9631a424c4070f9d423f7dc11 +yZijZHYe22y diff --git a/examples/closing-channels/closing-channels.sh b/examples/closing-channels/closing-channels.sh index 1958328..8d6baff 100644 --- a/examples/closing-channels/closing-channels.sh +++ b/examples/closing-channels/closing-channels.sh @@ -7,8 +7,7 @@ sent job 3 received job 3 sent all jobs received all jobs -no jobs to receive 0 -awaiting more jobs: false +received more jobs: false # 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 4f98cbf..331b79a 100644 --- a/public/closing-channels +++ b/public/closing-channels @@ -43,7 +43,7 @@ completion to the channel’s receivers.

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

-

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.

+

Reading from a closed channel succeeds immediately, +returning the zero value of the underlying type. +The optional second return value is true if the +value received was delivered by a successful send +operation to the channel, or false if it was a +zero value generated because the channel is closed +and empty.

-
    j, isOpened := <-jobs
-    fmt.Println("no jobs to receive", j)
-    fmt.Println("awaiting more jobs:", isOpened)
+          
    _, ok := <-jobs
+    fmt.Println("received more jobs:", ok)
 }
@@ -168,8 +170,7 @@ a false bool flag indicating that we should stop reading from it.

received job 3 sent all jobs received all jobs -no jobs to receive 0 -awaiting more jobs: false
+received more jobs: false @@ -200,7 +201,7 @@ example: range over channels.