Skip to content

Commit

Permalink
Added example of reading from closed channel (#498)
Browse files Browse the repository at this point in the history
* Add example of reading from closed channel

* Add second return value to read example

Also revert too complicated paragraph
  • Loading branch information
zhuboris committed Nov 30, 2023
1 parent 2943876 commit 991f893
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
8 changes: 8 additions & 0 deletions examples/closing-channels/closing-channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions examples/closing-channels/closing-channels.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
8f26c901e0f14df2ca40329a354c3ac86a5c3a07
vCvRjcMq7p3
3b474131d4d983ac5e53d8a6b94e069a8a4b775d
yLh6yhTGZeF
2 changes: 2 additions & 0 deletions examples/closing-channels/closing-channels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
26 changes: 22 additions & 4 deletions public/closing-channels

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 991f893

Please sign in to comment.