From 144421ac3c1f49b97980933d62dfd093835f0971 Mon Sep 17 00:00:00 2001 From: sang-w0o Date: Mon, 3 Jan 2022 17:12:13 +0900 Subject: [PATCH] fix: Wrong results on Closing-Channels --- examples/closing-channels/closing-channels.sh | 6 +- public/closing-channels | 193 +++++++++--------- 2 files changed, 101 insertions(+), 98 deletions(-) diff --git a/examples/closing-channels/closing-channels.sh b/examples/closing-channels/closing-channels.sh index 013f8a8..0eaee06 100644 --- a/examples/closing-channels/closing-channels.sh +++ b/examples/closing-channels/closing-channels.sh @@ -1,11 +1,11 @@ $ go run closing-channels.go sent job 1 -received job 1 sent job 2 -received job 2 sent job 3 -received job 3 sent all jobs +received job 1 +received job 2 +received job 3 received all jobs # The idea of closed channels leads naturally to our next diff --git a/public/closing-channels b/public/closing-channels index e4c94f5..b138588 100644 --- a/public/closing-channels +++ b/public/closing-channels @@ -1,97 +1,92 @@ - + Go by Example: Closing Channels - +

Go by Example: Closing Channels

- + - - + - + - + - + - + - + - + - + - + -
-

Closing a channel indicates that no more values -will be sent on it. This can be useful to communicate -completion to the channel’s receivers.

- -
- - +

+ Closing a channel indicates that no more values will be + sent on it. This can be useful to communicate completion to the + channel’s receivers. +

- - - -
package main
+            
+            
package main
 
- - - -
import "fmt"
+            
import "fmt"
 
-

In this example we’ll use a jobs channel to -communicate work to be done from the main() goroutine -to a worker goroutine. When we have no more jobs for -the worker we’ll close the jobs channel.

- +

+ In this example we’ll use a jobs channel to + communicate work to be done from the main() goroutine + to a worker goroutine. When we have no more jobs for the worker + we’ll close the jobs channel. +

- -
+            
 func main() {
     jobs := make(chan int, 5)
     done := make(chan bool)
 
-

Here’s the worker goroutine. It repeatedly receives -from jobs with j, more := <-jobs. In this -special 2-value form of receive, the more value -will be false if jobs has been closed and all -values in the channel have already been received. -We use this to notify on done when we’ve worked -all our jobs.

- +

+ Here’s the worker goroutine. It repeatedly receives from + jobs with j, more := <-jobs. In this + special 2-value form of receive, the more value will + be false if jobs has been + closed and all values in the channel have already + been received. We use this to notify on done when + we’ve worked all our jobs. +

- -
+            
     go func() {
         for {
             j, more := <-jobs
@@ -107,16 +102,16 @@ all our jobs.

-

This sends 3 jobs to the worker over the jobs -channel, then closes it.

- +

+ This sends 3 jobs to the worker over the + jobs channel, then closes it. +

- -
+            
     for j := 1; j <= 3; j++ {
         jobs <- j
         fmt.Println("sent job", j)
@@ -126,73 +121,81 @@ channel, then closes it.

-

We await the worker using the -synchronization approach -we saw earlier.

- +

+ We await the worker using the + synchronization approach we + saw earlier. +

- -
+            
     <-done
 }
 
- + - - + - + - + -
- - - -
$ go run closing-channels.go 
+            
$ go run closing-channels.go 
 sent job 1
-received job 1
 sent job 2
-received job 2
 sent job 3
-received job 3
 sent all jobs
+received job 1
+received job 2
+received job 3
 received all jobs
-

The idea of closed channels leads naturally to our next -example: range over channels.

- -
- - +

+ The idea of closed channels leads naturally to our next example: + range over channels. +

- - +

Next example: Range over Channels.

- - - +