
2 min read
Limiting goroutines, chapter 2
Last time I introduced the SetLimit feature of the errgroup package. But this feature is not sufficient if you ever ant to set some sort of shared or global limit on the number of jobs doing a task. For a more general-purpose rate limiting mechanism, I often reach for the x/sync/semaphore package. Let’s modify the example from last time, to introduce a global limit using this library: import ( // other impots "golang.

2 min read
Limiting goroutines
The errgroup package we’ve been looking at lately has one more trick up its sleeve: Limiting the number of running goroutines. Let’s imagine we’re building an HTTP handler that responds with a list of all customer orders. Doing so, requires making a number of calls to the ‘orders’ microservice. Some customers may have made hundreds or thousands of orders. We don’t want to look them all up simultaneously, so we’ll limit our client to 10 concurrent requests.

1 min read
Aborting goroutines on error
I’m sorry for the delay in writing. Last week I was under the weather. Let’s pick up our discussion of goroutines and errors. Last time, I talked about errgroup, which makes it easy to run an arbitrary number of goroutines, and gather the first error (if any) that one returns. But what if that error renders the rest of the goroutines obsolete? A very common case is that any error means that the rest of the processing becomes moot.

2 min read
Simpler error handling in goroutines
Join me later today at 3:00pm EDT (19:00 UTC), for more live coding. Subscribe on YouTube and hit the Notify me button so you don’t miss it! Yesterday we looked at how to handle errors from multiple goroutines using an error channel. While functional, the approach is somewhat complicated, and easy to get wrong. Enter errgroup. While not part of the standard library, it is maintained by the Go team, and is widely used in the Go ecosystem.
Subscribe to Boldly Go: Daily
Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.
Unsure? Browse the archive.

3 min read
Goroutines and error channels
Join me tomorrow, August 29, at 3:00pm EDT (19:00 UTC), for more live coding. Subscribe on YouTube and hit the Notify me button so you don’t miss it! Last week we looked at how to handle errors from multiple goroutines using a mutex and a shared error variable. While this works for many common scenarios, it has some limitations. For example, it only captures the first error encountered and ignores any subsequent errors.

3 min read
But what about errors?
Join me today, August 22, at 3:00pm EDT (19:00 UTC), more live coding. Subscribe on YouTube and hit the Notify me button so you don’t miss it! We’ve been looking at different ways of managing multiple goroutines. But what if one (or more) the functions called in a goroutine might return an error? So far, we don’t have a solution to that in our toolbox. There are a few different ways we can accomplish this.

2 min read
Another solution to the WaitGroup mistake
Join me again, this coming Friday, August 22, at 3:00pm EDT (19:00 UTC), more live coding. Subscribe on YouTube and hit the Notify me button so you don’t miss it! Last week we looked at a common mistake with sync.WaitGroup, and how Go 1.25’s new go vet check can help catch it. To recap, the mistake is calling wg.Add(1) from within a goroutine, which can cause wg.Wait() to return prematurely:

2 min read
A common WaitGroup mistake
Can you spot the mistake in this code? for i := range 100 { go func() { wg.Add(1) defer wg.Done() fmt.Println(i) }() } wg.Wait() At first glance, this code probably looks fine. We’re calling wg.Add(1) at the beginning of each task. We’re deferring the call to wg.Done(), so it’s called when the function exits. Each Add call has a matching Done call. We’re properly waiting for all tasks to complete with wg.

2 min read
Dynamic wait groups
Join me this Friday, August 15, at 3:00pm EDT (19:00 UTC), for a live stream where I’ll be building a new feature in my open-source project. Subscribe on YouTube and hit the Notify me button so you don’t miss it! One thing we can do with sync.WaitGroup, that may not be immediately obvious, is to call Add from within a goroutine that’s managed by the wait group itself: wg := &sync.

2 min read
WaitGroup shortcuts
Join me next Friday, at 3:00pm EDT (19:00 UTC), for a live stream where I’ll be building a new feature in my open-source project. Subscribe on YouTube and hit the Notify me button so you don’t miss it! Yesterday I introduced the sync.WaitGroup type. Today I want to look at some possible ways to improve the same code. First, the code as we left off yesterday: wg := &sync.WaitGroup{} for i := range 10 { wg.

2 min read
sync.WaitGroup
It’s been a year since my last live stream, as I was moving across the globe. I’m finally settled (enough) to start up again. Join me, as I’ll begin building a new feature in my open-source project. The next session will be August 15, at 19:00 UTC. Subscribe on YouTube and hit the Notify me button so you don’t miss it! Yesterday we wrote some Go code to wait for goroutines using a channel: