daily pastebin goal
15%
SHARE
TWEET

Untitled

a guest Nov 20th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2. import (
  3.     "fmt"
  4.     "sync"
  5. )
  6. func main() {
  7.     var wg sync.WaitGroup
  8.     done := make(chan struct{})
  9.     wq := make(chan interface{})
  10.     workerCount := 2
  11.     for i := 0; i < workerCount; i++ {
  12.         wg.Add(1)
  13.         go doit(i,wq,done,&wg)
  14.     }
  15.     for i := 0; i < workerCount; i++ {
  16.         wq <- i
  17.     }
  18.     close(done)
  19.     wg.Wait()
  20.     fmt.Println("all done!")
  21. }
  22. func doit(workerId int, wq <-chan interface{},done <-chan struct{},wg *sync.WaitGroup) {
  23.     fmt.Printf("[%v] is running\n",workerId)
  24.     defer wg.Done()
  25.     for {
  26.         select {
  27.         case m := <- wq:
  28.             fmt.Printf("[%v] m => %v\n",workerId,m)
  29.         case <- done:
  30.             fmt.Printf("[%v] is done\n",workerId)
  31.             return
  32.         }
  33.     }
  34. }
RAW Paste Data
Top