Copyright 2016 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Package errgroup provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.
package errgroup

import (
	
	
)
A Group is a collection of goroutines working on subtasks that are part of the same overall task. A zero Group is valid and does not cancel on error.
type Group struct {
	cancel func()

	wg sync.WaitGroup

	errOnce sync.Once
	err     error
}
WithContext returns a new Group and an associated Context derived from ctx. The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first.
func ( context.Context) (*Group, context.Context) {
	,  := context.WithCancel()
	return &Group{cancel: }, 
}
Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.
func ( *Group) () error {
	.wg.Wait()
	if .cancel != nil {
		.cancel()
	}
	return .err
}
Go calls the given function in a new goroutine. The first call to return a non-nil error cancels the group; its error will be returned by Wait.
func ( *Group) ( func() error) {
	.wg.Add(1)

	go func() {
		defer .wg.Done()

		if  := ();  != nil {
			.errOnce.Do(func() {
				.err = 
				if .cancel != nil {
					.cancel()
				}
			})
		}
	}()