errgroup

package module
v0.0.0-...-67d2de4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 4 Imported by: 0

README

errgroup

Overview

errgroup is a Go module that provides Context cancellation, error propagation and synchronisation for goroutines running fallible functions.

Usage

Creating an errgroup

As per the Go proverb, the zero value of the errgroup.Group is useful, and you can simply create a errgroup.Group as follows and begin using it:

var eg errgroup.Group

You can also configure the behaviour of an errgroup.Group by providing some errgroup.Configurer's to the errgroup.New function. For example:

var (
    ctx, cc = errgroup.WithCancel(ctx.Background())
    eg      = errgroup.New(cc)
)
Using an errgroup

Once you have created an errgroup.Group, you can begin using it by calling errgroup.Group.Go. Then, you can call errgroup.Group.Wait to wait for the result:

var fs []func() error

// ...

for _, f := range fs {
    _ = eg.Go(f)
}

errs := eg.Wait()
fmt.Println(errs)

Documentation

Documentation for errgroup can be found here.

Documentation

Overview

Package errgroup is a Go package that provides Context cancellation, error propagation and synchronisation for goroutines running fallible functions.

Index

Constants

View Source
const Version = "0.0.0"

Variables

This section is empty.

Functions

This section is empty.

Types

type CancelError

type CancelError struct{}

CancelError indicates that the Group has been cancelled.

func (CancelError) Error

func (c CancelError) Error() string

type Configurer

type Configurer interface {
	// contains filtered or unexported methods
}

Configurer configures the behaviour of a Group.

func WithCancel

func WithCancel(ctx context.Context) (context.Context, Configurer)

WithCancel returns a context.Context (derived from ctx) and a Configurer. The returned Configurer configures a Group to cancel the derived context.Context when:

  • The first time a function passed to Group.Go returns a non-nil error.
  • The first time a call to Group.Wait returns.

func WithRunner

func WithRunner(runner Runner) Configurer

WithRunner returns a Configurer that configures a Group to run every f supplied to Group.Go using the supplied Runner.

type GoRunner

type GoRunner struct{}

GoRunner is a Runner that runs f in another goroutine that was spawned using the `go` keyword.

func (*GoRunner) Run

func (r *GoRunner) Run(f func()) error

type Group

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

Group manages the execution of fallible functions i.e. functions of type func() error.

func New

func New(configurers ...Configurer) *Group

New returns a new Group that has been configured by applying the supplied Configurer's.

func (*Group) Go

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

Go runs f according to the semantics of the Group's Runner, or it returns a CancelError if the Group has been cancelled.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until the Group has run every f supplied to Group.Go and returns an error that aggregates any errors that occurred.

type Runner

type Runner interface {
	Run(f func()) error
}

Runner runs f in a separate goroutine.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL