Go work groups

24 Feb 2017

One of those blog entries where I just want to remember something useful.

func main() {
  wg := &sync.WaitGroup{}
  // launch 10 worker goroutines
  for i := 0; i < 10; i++ {
    wg.Add(1)  // must happen outside doWork
    go doWork(wg)
  }
  wg.Wait()  // will wait for all workers to finish
}

func doWork(wg *sync.WaitGroup) {
  defer wg.Done() // think of this as wg.Minus(1)
  // do some actual work in here
}