Created
May 29, 2018 00:45
-
-
Save serinth/d0a747413efafc4a2abc6a57a1f25b4c to your computer and use it in GitHub Desktop.
Wait Group Example - Wait for all Go Routines to Finish before resuming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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