errgroup

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package errgroup provides synchronization, error propagation, and Context cancellation for groups of goroutines working on subtasks of a common task. It wraps golang.org/x/sync/errgroup, with the addition that goroutines launched via Group.Go and Group.TryGo are wrapped with calm.Unpanic, so any panic is recovered and returned as an error rather than crashing the program.

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
}

Group is a collection of goroutines working on subtasks that are part of the same overall task.

A zero Group is valid and does not cancel on error by default. A Group should not be reused for different tasks.

func New

func New() *Group

New returns a new Group with no associated context.

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. Panics inside f are recovered by calm.Unpanic and returned as errors.

The first call to return a non-nil error (or panic) cancels the group's context, if the group was created by calling WithContext. The error is then returned by Wait.

Go blocks until the new goroutine can be added without exceeding the configured limit.

Example
package main

import (
	"fmt"

	"github.com/wood-jp/xerrors/errclass"
	"github.com/wood-jp/xerrors/errgroup"
)

func main() {
	g := errgroup.New()
	g.Go(func() error {
		panic("something went wrong")
	})
	err := g.Wait()
	fmt.Println(errclass.GetClass(err))
}
Output:
panic

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 zero value prevents new goroutines from starting.

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. Panics inside f are recovered by calm.Unpanic and returned as errors.

The return value reports whether the goroutine was started. If TryGo would exceed the group's limit, it returns false without calling f.

func (*Group) Wait

func (g *Group) Wait() error

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

Jump to

Keyboard shortcuts

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