Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example of reading from closed channel #498

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading