Skip to content

Instantly share code, notes, and snippets.

@StarpTech
Created January 28, 2018 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StarpTech/342dabb9b39126bc9de8d9e179becde9 to your computer and use it in GitHub Desktop.
Save StarpTech/342dabb9b39126bc9de8d9e179becde9 to your computer and use it in GitHub Desktop.
When a channel is closed all buffered and blocked messages are proceed until the "range is finish
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