Created
January 28, 2018 11:47
-
-
Save StarpTech/4dcd2e61317d8dc3ede29fa309a4a000 to your computer and use it in GitHub Desktop.
When a channel is closed sent messages are proceed until the "range" is closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
ch := make(chan int, 100) | |
wg.Add(2) | |
go func() { | |
defer wg.Done() | |
for a := range ch { | |
fmt.Println("Receive job in Worker 1", a) | |
time.Sleep(time.Millisecond * 100) | |
} | |
}() | |
go func() { | |
defer wg.Done() | |
for a := range ch { | |
fmt.Println("Receive job in Worker 2", a) | |
time.Sleep(time.Millisecond * 100) | |
} | |
}() | |
for i := 0; i < 100; i++ { | |
ch <- i | |
fmt.Println("sent job", i) | |
} | |
close(ch) | |
wg.Wait() | |
fmt.Println("Done!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment