Fix up formatting and phrasing for closed chan read

This commit is contained in:
Eli Bendersky
2023-11-30 05:08:42 -08:00
parent 991f8937dd
commit 461f6955d6
4 changed files with 24 additions and 22 deletions

View File

@@ -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)
}

View File

@@ -1,2 +1,2 @@
3b474131d4d983ac5e53d8a6b94e069a8a4b775d
yLh6yhTGZeF
13f0ccf3674db8e9631a424c4070f9d423f7dc11
yZijZHYe22y

View File

@@ -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.