Go Deep Dive
Errgroup Explained: Understanding Its Inner Workings
We’ve delved into using errgroup to boost the performance of independent tasks, thanks to the robust features it offers, these include:
To get the most out of our discussion, it’s important to have a good grasp of two key concepts in Go programming: errgroup and channels.
- Go Errgroup: You Haven’t Used Goroutines Effectively Unless You’ve Used This Package
- Go Channels Explained: More Than Just a Beginner’s Guide.
Once you’re comfortable with these concepts, you’ll find it much easier to dive into the more advanced stuff we’re going to talk about.
I aim to make the topic more accessible and valuable for you, drawing from my personal experiences and understanding.
1. What is inside the errgroup?
At the heart of the errgroup package lies the Group
struct. Let's take a closer look at its structure:
type Group struct {
cancel func(error)
wg sync.WaitGroup
sem chan token
errOnce sync.Once
err error
}
This struct employs two synchronization techniques: sync.WaitGroup
and sync.Once
.
I’ve covered these in detail in my article ‘6 Key Sync Package Concepts’, but here’s a brief recap:
sync.WaitGroup
: It’s used to wait for a group of goroutines to complete their execution.sync.Once
: This ensures that certain initialization code is executed only once, no matter how many times it’s called. It’s safe to use concurrently.
We also have an error
field, we only capture the first error and ignore any subsequent ones.
The cancel
field is a function and it's used to cancel the context either when the task is completed or when an error occurs.
What really makes this struct special is the sem
channel, which acts as a semaphore which is crucial for controlling the number of goroutines that run at the same time.