Documentation

Overview

    Package workgroup provides a mechanism for controlling the lifetime of a set of related goroutines.

    Index

    Examples

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    This section is empty.

    Types

    type Group

    type Group struct {
    	// contains filtered or unexported fields
    }

      A Group manages a set of goroutines with related lifetimes. The zero value for a Group is fully usable without initialisation.

      func (*Group) Add

      func (g *Group) Add(fn func(<-chan struct{}) error)

        Add adds a function to the Group. The function will be executed in its own goroutine when Run is called. Add must be called before Run.

        func (*Group) AddContext

        func (g *Group) AddContext(fn func(context.Context) error)

          AddContext adds a function taking a context.Context to the group. The function will be executed in its own goroutine when Run is called. The context supplied to the function will be canceled when the group exits. AddContext must be called before Run.

          func (*Group) Run

          func (g *Group) Run(ctx context.Context) error

            Run executes each function registered via Add in its own goroutine. Run blocks until all functions have returned. The first function to return will trigger the closure of the channel passed to each function, who should in turn, return. The return value from the first function to exit will be returned to the caller of Run. If ctx is canceled, Run will return with the corresponding error.

            Example
            Output:
            
            A stopped
            B stopped
            timed out
            
            Example (MultipleListeners)
            Output:
            
            
            Example (WithShutdown)
            Output:
            
            shutdown
            

            Source Files