Help us shape the future of Go! Take the 2026 Go Developer Survey

errgroup

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group interface {
	// Go calls the given function in a new goroutine.
	//
	// The first call to return a non-nil error cancels the group; its error will be
	// returned by Wait.
	Go(fn func(ctx context.Context) error)

	// GOMAXPROCS set max goroutine to work.
	GOMAXPROCS(n int)

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

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

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

Example (JustErrors)

JustErrors illustrates the use of a Group in place of a sync.WaitGroup to simplify goroutine counting and error handling. This example is derived from the sync.WaitGroup example at https://golang.org/pkg/sync/#example_WaitGroup.

eg := WithContext(context.Background())
urls := []string{
	"http://www.golang.org/",
	"http://www.google.com/",
	"http://www.somestupidname.com/",
}
for _, _url := range urls {
	// Launch a goroutine to fetch the URL.
	url := _url // https://golang.org/doc/faq#closures_and_goroutines
	eg.Go(func(context.Context) error {
		// Fetch the URL.
		resp, err := http.Get(url)
		if err == nil {
			resp.Body.Close()
		}
		return err
	})
}
// Wait for all HTTP fetches to complete.
if err := eg.Wait(); err == nil {
	fmt.Println("Successfully fetched all URLs.")
}
Example (Parallel)

Parallel illustrates the use of a Group for synchronizing a simple parallel task: the "Google Search 2.0" function from https://talks.golang.org/2012/concurrency.slide#46, augmented with a Context and error-handling.

Google := func(ctx context.Context, query string) ([]Result, error) {
	eg := WithContext(context.Background())

	searches := []Search{Web, Image, Video}
	results := make([]Result, len(searches))
	for _i, _search := range searches {
		i, search := _i, _search // https://golang.org/doc/faq#closures_and_goroutines
		eg.Go(func(context.Context) error {
			result, err := search(ctx, query)
			if err == nil {
				results[i] = result
			}
			return err
		})
	}
	if err := eg.Wait(); err != nil {
		return nil, err
	}
	return results, nil
}

results, err := Google(context.Background(), "golang")
if err != nil {
	fmt.Fprintln(os.Stderr, err)
	return
}
for _, result := range results {
	fmt.Println(result)
}
Output:
web result for "golang"
image result for "golang"
video result for "golang"

func WithContext

func WithContext(ctx context.Context) Group

WithContext returns a new group with a canceled 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.

Jump to

Keyboard shortcuts

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