// Package task provides an alternative to errgroup package with builtin context// cancellation support.//// This packages uses go.uber.org/multierr to combine errors from failed tasks.
package taskimport ()// Task is a function that performs some work in foreground.typeTaskfunc(ctx context.Context) error// Run executes the task.func ( Task) ( context.Context) error {return ()}// Named returns a task that returns an error prefixed with name on failure.func ( string, Task) Task {returnfunc( context.Context) error { := ()if == nil {returnnil }returnfmt.Errorf("%s: %w", , ) }}// Sequential returns a function that executes a group of tasks sequentially// using the given cancellation condition. The resulting Task returns combined// errors for all failed subtasks.func ( CancelCondition, ...Task) Task {returnfunc( context.Context) error {var []error , := context.WithCancel()defer ()for , := range { := .Run()if != nil {if == nil { = make([]error, len()) } [] = }if () { () } }returnmultierr.Combine(...) }}// Parallel returns a function that executes a group of tasks in parallel using// the given cancellation condition. The resulting Task returns combined errors// for all failed subtasks.func ( CancelCondition, ...Task) Task {returnfunc( context.Context) error {varsync.Oncevar []error , := context.WithCancel()defer ()varsync.WaitGroup .Add(len())for , := range { , := , gofunc() {defer .Done() := .Run()if != nil { .Do(func() { = make([]error, len()) }) [] = }if () { () } }() } .Wait()returnmultierr.Combine(...) }}
The pages are generated with Goldsv0.4.9. (GOOS=linux GOARCH=amd64)