diff --git a/examples/closing-channels/closing-channels.go b/examples/closing-channels/closing-channels.go index 926d9b51e..edc86d866 100644 --- a/examples/closing-channels/closing-channels.go +++ b/examples/closing-channels/closing-channels.go @@ -47,4 +47,12 @@ func main() { // [synchronization](channel-synchronization) approach // 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) } diff --git a/examples/closing-channels/closing-channels.hash b/examples/closing-channels/closing-channels.hash index 3e33a0715..41fbccafe 100644 --- a/examples/closing-channels/closing-channels.hash +++ b/examples/closing-channels/closing-channels.hash @@ -1,2 +1,2 @@ -8f26c901e0f14df2ca40329a354c3ac86a5c3a07 -vCvRjcMq7p3 +3b474131d4d983ac5e53d8a6b94e069a8a4b775d +yLh6yhTGZeF diff --git a/examples/closing-channels/closing-channels.sh b/examples/closing-channels/closing-channels.sh index 013f8a879..195832850 100644 --- a/examples/closing-channels/closing-channels.sh +++ b/examples/closing-channels/closing-channels.sh @@ -7,6 +7,8 @@ sent job 3 received job 3 sent all jobs received all jobs +no jobs to receive 0 +awaiting 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 a6310979d..4f98cbf67 100644 --- a/public/closing-channels +++ b/public/closing-channels @@ -43,7 +43,7 @@ completion to the channel’s receivers.
package main
<-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.
+ <-done
+ j, isOpened := <-jobs
+ fmt.Println("no jobs to receive", j)
+ fmt.Println("awaiting more jobs:", isOpened)
}
range
over channels.