Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.