...

Source file src/github.com/hashicorp/go-multierror/group.go

Documentation: github.com/hashicorp/go-multierror

     1  package multierror
     2  
     3  import "sync"
     4  
     5  // Group is a collection of goroutines which return errors that need to be
     6  // coalesced.
     7  type Group struct {
     8  	mutex sync.Mutex
     9  	err   *Error
    10  	wg    sync.WaitGroup
    11  }
    12  
    13  // Go calls the given function in a new goroutine.
    14  //
    15  // If the function returns an error it is added to the group multierror which
    16  // is returned by Wait.
    17  func (g *Group) Go(f func() error) {
    18  	g.wg.Add(1)
    19  
    20  	go func() {
    21  		defer g.wg.Done()
    22  
    23  		if err := f(); err != nil {
    24  			g.mutex.Lock()
    25  			g.err = Append(g.err, err)
    26  			g.mutex.Unlock()
    27  		}
    28  	}()
    29  }
    30  
    31  // Wait blocks until all function calls from the Go method have returned, then
    32  // returns the multierror.
    33  func (g *Group) Wait() *Error {
    34  	g.wg.Wait()
    35  	g.mutex.Lock()
    36  	defer g.mutex.Unlock()
    37  	return g.err
    38  }
    39  

View as plain text