Documentation ¶
Overview ¶
Example ¶
wg := &WaitGroup{} wg.Add(2) go func() { log.Println("goroutine 1!") wg.Done(nil) }() go func() { log.Println("goroutine 2!") wg.Done(nil) }() if err := wg.Wait(); err != nil { log.Fatal(err) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.
A WaitGroup must not be copied after first use.
WaitGroup is distinct from sync.WaitGroup because it collects errors returned by the goroutines in a multierror.Error.
func (*WaitGroup) Add ¶
Add adds delta, which may be negative, to the WaitGroup counter. If the counter becomes zero, all goroutines blocked on Wait are released. If the counter goes negative, Add panics.
Note that calls with a positive delta that occur when the counter is zero must happen before a Wait. Calls with a negative delta, or calls with a positive delta that start when the counter is greater than zero, may happen at any time. Typically this means the calls to Add should execute before the statement creating the goroutine or other event to be waited for. A WaitGroup should not be reused to wait for several independent sets of events because the collected errors can not be reset