...

Package errgroup

import "cmd/vendor/golang.org/x/sync/errgroup"
Overview
Index

Overview ▾

Package errgroup provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.

errgroup.Group is related to sync.WaitGroup but adds handling of tasks returning errors.

type Group

A Group is a collection of goroutines working on subtasks that are part of the same overall task. A Group should not be reused for different tasks.

A zero Group is valid, has no limit on the number of active goroutines, and does not cancel on error.

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

func WithContext

func WithContext(ctx context.Context) (*Group, context.Context)

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 (*Group) Go

func (g *Group) Go(f func() error)

Go calls the given function in a new goroutine.

The first call to Go must happen before a Wait. It blocks until the new goroutine can be added without the number of goroutines in the group exceeding the configured limit.

The first goroutine in the group that returns a non-nil error, panics, or invokes runtime.Goexit will cancel the associated Context, if any.

func (*Group) SetLimit

func (g *Group) SetLimit(n int)

SetLimit limits the number of active goroutines in this group to at most n. A negative value indicates no limit. A limit of zero will prevent any new goroutines from being added.

Any subsequent call to the Go method will block until it can add an active goroutine without exceeding the configured limit.

The limit must not be modified while any goroutines in the group are active.

func (*Group) TryGo

func (g *Group) TryGo(f func() error) bool

TryGo calls the given function in a new goroutine only if the number of active goroutines in the group is currently below the configured limit.

The return value reports whether the goroutine was started.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all function calls from the Go method have returned normally, then returns the first non-nil error (if any) from them.

If any of the calls panics, Wait panics with a PanicValue; and if any of them calls runtime.Goexit, Wait calls runtime.Goexit.

type PanicError

PanicError wraps an error recovered from an unhandled panic when calling a function passed to Go or TryGo.

type PanicError struct {
    Recovered error
    Stack     []byte // result of call to [debug.Stack]
}

func (PanicError) Error

func (p PanicError) Error() string

func (PanicError) Unwrap

func (p PanicError) Unwrap() error

type PanicValue

PanicValue wraps a value that does not implement the error interface, recovered from an unhandled panic when calling a function passed to Go or TryGo.

type PanicValue struct {
    Recovered any
    Stack     []byte // result of call to [debug.Stack]
}

func (PanicValue) String

func (p PanicValue) String() string