Back to Gohacks
Gohacks

Parallel

A Go library for managing concurrent execution with a goroutine pool. Control concurrency, limit goroutines, and keep your code simple.

Add to your Go project:

go get github.com/gohacks/parallel

Why Parallel?

Running too many goroutines at once can exhaust resources. Parallel gives you a simple pool with a configurable concurrency limit — add tasks, and the library manages the rest.

Semaphore-based — Uses channels to limit concurrent goroutines. No external dependencies.
Simple API New(), Run(), Wait(). That's it.
Error handling — Optional cancel-on-error. Collect errors with parallel.Errors().

Installation

Add to your Go module:

go get github.com/gohacks/parallel

Usage

pool := parallel.New(context.Background(), 3, false)
for i := 1; i <= 10; i++ {
    pool.Run(func() { yourFunction(yourParams) })
}
pool.Wait()

API

Function Description
New(ctx, max, cancelOnError)📋 Creates a pool with max concurrent goroutines
pool.Run(task)📋 Adds a task to the pool
pool.Wait()📋 Waits for all tasks to complete
parallel.Errors()📋 Returns errors collected from tasks