Skip to main content

The errgroup Package: Your secret weapon for managing concurrent goroutines in Go

Are you tired of managing the concurrent execution of multiple goroutines in Go, only to have them fail and ruin your entire day? Well, fear not dear reader, because the errgroup package is here to save the day!

This small, open-source library provides a simple and convenient way to run a group of independent tasks concurrently, and then wait for all of them to complete (or for one of them to fail and ruin everything). All you have to do is create a new errgroup.Group object and pass it to the functions that you want to run concurrently. Each of these functions should return an error value, which will be captured by the errgroup.Group object like a little error-catching ninja.

Here’s an example of how to use the errgroup package to concurrently fetch the contents of multiple URLs and then cry in despair if any of them fail:

package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"golang.org/x/sync/errgroup"
)

func main() {
	urls := []string{
		"https://www.example.com/",
		"https://www.example.net/",
		"https://www.example.org/",
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
	defer cancel()

	g, ctx := errgroup.WithContext(ctx)

	for _, url := range urls {
		url := url // capture range variable
		g.Go(func() error {
			resp, err := http.Get(url)
			if err != nil {
				return err
			}
			defer resp.Body.Close()

			_, err = ioutil.ReadAll(resp.Body)
			return err
		})
	}

	if err := g.Wait(); err != nil {
		fmt.Println("Oh no, something went wrong :(")
		fmt.Println(err)
	}
}

As you can see, using the errgroup package is as easy as pie (or perhaps even easier, depending on your pie-making skills). And the best part is, if any of the goroutines fail, the Wait() method will return that error and you can go cry in a corner (or at least, that’s what I do).

So don’t let concurrent goroutines ruin your day anymore, give the errgroup package a try and enjoy the peace of mind that comes with knowing that your tasks are being managed and your errors are being caught. Happy coding!

comments powered by Disqus