Skip to content

Instantly share code, notes, and snippets.

@serinth
Created May 29, 2018 00:45
Show Gist options
  • Save serinth/d0a747413efafc4a2abc6a57a1f25b4c to your computer and use it in GitHub Desktop.
Save serinth/d0a747413efafc4a2abc6a57a1f25b4c to your computer and use it in GitHub Desktop.
Wait Group Example - Wait for all Go Routines to Finish before resuming
errorChannel := make(chan error)
var wg sync.WaitGroup
funcsToRun := []func(ctx context.Context) error {
implementation.func1,
implementation.func2,
implementation.func3,
}
wg.Add(len(funcsToRun))
for _, f := range funcsToRun {
go func(fn func(ctx context.Context) error) {
defer wg.Done()
errorChannel <- fn(ctx)
}(f)
}
go func() {
for e := range errorChannel {
if e != nil {
log.Errorf("Error when running func: %v", e)
}
}
}()
wg.Wait()
log.Info("All routines completed, continuing...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment