Skip to content

Instantly share code, notes, and snippets.

@polefishu
Created April 10, 2017 15:42
Show Gist options
  • Save polefishu/c47a2717470e7bd649a68815f74978c6 to your computer and use it in GitHub Desktop.
Save polefishu/c47a2717470e7bd649a68815f74978c6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
"sync"
)
func worker(wg *sync.WaitGroup, cs chan string, i int) {
defer wg.Done()
cs <- "worker" + strconv.Itoa(i)
}
func monitorWorker(wg *sync.WaitGroup, cs chan string) {
wg.Wait()
close(cs)
}
func printWorker(cs <-chan string, done chan<- bool) {
for i := range cs {
fmt.Println(i)
}
done <- true
}
func main() {
wg := &sync.WaitGroup{}
cs := make(chan string)
for i := 0; i < 10; i++ {
wg.Add(1)
go worker(wg, cs, i)
}
go monitorWorker(wg, cs)
done := make(chan bool, 1)
go printWorker(cs, done)
<-done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment