In this post, I'll show you how you can combine the elegance of a WaitGroup with the buffering of channels. This way, you can use a sync.WaitGroup but still control the concurrency.

A sync.WaitGroup is a very nice concept in Go and is simple to use. However, the stock sync.WaitGroup just launches all the goroutines at once without letting you have any control over the concurrency.

Buffered channels on the other hand allow you to control the concurrency, but the syntax is a bit hard and more verbose than using a sync.WaitGroup.

In this following code sample, we show a way to combine the best of both by creating a custom WaitGroup which implements a goroutine pool and allows us to control the concurrency.

package waitgroup

import (

// WaitGroup implements a simple goruntine pool.
type WaitGroup struct {
    size      int
    pool      chan byte
    waitGroup sync.WaitGroup

// NewWaitGroup creates a waitgroup with a specific size (the maximum number of
// goroutines to run at the same time). If you use -1 as the size, all items 
// will run concurrently (just like a normal sync.WaitGroup)
func NewWaitGroup(size int) *WaitGroup {
    wg := &WaitGroup{
        size: size,
    if size > 0 {
        wg.pool = make(chan byte, size)
    return wg

// BlockAdd pushes ‘one’ into the group. Blocks if the group is full.
func (wg *WaitGroup) BlockAdd() {
    if wg.size > 0 {
        wg.pool <- 1

// Done pops ‘one’ out the group.
func (wg *WaitGroup) Done() {
    if wg.size > 0 {

// Wait waiting the group empty
func (wg *WaitGroup) Wait() {

Using it is just like a normal sync.WaitGroup. The only difference is the initialisation. When you use waitgroup.NewWaitGroup, you have the option to specify it's size.

Any int which is bigger than 0 will limit the number of concurrent goroutines. If you specify -1 or 0, all goroutines will run at once (just like a plain sync.WaitGroup).

package main

import (

func main() {

    urls := []string{

    wg := waitgroup.NewWaitGroup(3)

    for _, url := range urls {
        go func(url string) {
            fmt.Println("%s: checking", url)
            res, err := http.Get(url)
            if err != nil {
                fmt.Println("Error: %v")
            } else {
                defer res.Body.Close()
                fmt.Println("%s: result: %v", err)



You can import the package from

Related Posts

  • Graceful shutdown: handling CTRL+C
  • Using the Docker client from Go part 2
  • The beauty of io.Reader in Go
  • Golang vs Apple Silicon
  • Pretty-print JSON with Go