workers

June 3, 2026 ยท View on GitHub

simple worker pool

GoDoc Build Status codecov License

The workers package implements a simple fixed-size pool of goroutines for executing functions concurrently. The purpose is to limit concurrency to the number of goroutines in the pool.

When all workers are busy, the channel for submitting tasks blocks. If you are looking for a worker pool that never blocks when submitting tasks, see workerpool.

Installation

$ go get github.com/gammazero/workers

Example

package main

import (
	"fmt"
	"github.com/gammazero/workers"
)

func main() {
	do, done := workers.New(5)
	requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}

	for _, r := range requests {
		do <- func() {
			fmt.Println("Handling request:", r)
		}
	}
	close(do) // stop workers
	<-done    // wait for all workers to exit

	fmt.Println("All done")
}