1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package errgroup provides synchronization, error propagation, and Context
6 // cancelation for groups of goroutines working on subtasks of a common task.
14 // A Group is a collection of goroutines working on subtasks that are part of
15 // the same overall task.
17 // A zero Group is valid and does not cancel on error.
27 // WithContext returns a new Group and an associated Context derived from ctx.
29 // The derived Context is canceled the first time a function passed to Go
30 // returns a non-nil error or the first time Wait returns, whichever occurs
32 func WithContext(ctx context.Context) (*Group, context.Context) {
33 ctx, cancel := context.WithCancel(ctx)
34 return &Group{cancel: cancel}, ctx
37 // Wait blocks until all function calls from the Go method have returned, then
38 // returns the first non-nil error (if any) from them.
39 func (g *Group) Wait() error {
47 // Go calls the given function in a new goroutine.
49 // The first call to return a non-nil error cancels the group; its error will be
51 func (g *Group) Go(f func() error) {
57 if err := f(); err != nil {