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.
12 "golang.org/x/net/context"
15 // A Group is a collection of goroutines working on subtasks that are part of
16 // the same overall task.
18 // A zero Group is valid and does not cancel on error.
28 // WithContext returns a new Group and an associated Context derived from ctx.
30 // The derived Context is canceled the first time a function passed to Go
31 // returns a non-nil error or the first time Wait returns, whichever occurs
33 func WithContext(ctx context.Context) (*Group, context.Context) {
34 ctx, cancel := context.WithCancel(ctx)
35 return &Group{cancel: cancel}, ctx
38 // Wait blocks until all function calls from the Go method have returned, then
39 // returns the first non-nil error (if any) from them.
40 func (g *Group) Wait() error {
48 // Go calls the given function in a new goroutine.
50 // The first call to return a non-nil error cancels the group; its error will be
52 func (g *Group) Go(f func() error) {
58 if err := f(); err != nil {