Go's standard library has no batch framework, but goroutines, channels, sync, context and the iter and slices packages are enough to build reliable batch jobs and data pipelines. The usual pattern is a pipeline of stages connected by channels, with a bounded worker pool in the slow stage, context for cancellation, and errors or errgroup style handling for failures.
This article shows the main patterns with code that compiles on current Go, and calls out the mistakes that older examples make.
slices.Chunk (Go 1.23 and later) splits a slice into consecutive batches without copying:
Batching matters because most bottlenecks, such as database inserts or API calls, are cheaper per item when sent in groups.
Starting one goroutine per batch works for ten batches but not for ten million. A fixed pool limits memory and load on downstream systems. Results and errors are collected without losing any of them.
Key points: the producer closes batches so workers exit, the error channel is buffered so workers never block on it, and errors.Join (Go 1.20 and later) combines every failure into one error. A batch that fails does not stop the others, which suits jobs where partial success is acceptable.
A note on older code: before Go 1.22, a goroutine started inside a for loop that used the loop variable could see the wrong value. Since Go 1.22 each iteration has its own variable, but passing values as arguments is still clear.
A pipeline connects stages through channels. Each stage stops when the context is cancelled, so no goroutine leaks if a later stage fails or the caller times out.
Every send uses select with ctx.Done(). Without it, a stage blocked on an unread channel would never exit.
For each unit of work, decide whether a failure is fatal, skippable or retryable:
fmt.Errorf("batch %d: %w", id, err) so callers can use errors.Is and errors.As.golang.org/x/sync/errgroup, which cancels a shared context on the first error.Read files line by line with bufio.Scanner or record by record with encoding/csv instead of loading everything into memory. Combine that with the worker pool above so memory use depends on the pool size, not the file size.
Remember to call scanner.Err() after the loop, and increase the scanner buffer with scanner.Buffer if lines can exceed 64 KB.
The standard library does not provide scheduling, persistence, retries across restarts or distributed coordination. For recurring or long-running jobs, use a scheduler such as Kubernetes CronJobs, or a workflow engine such as Temporal. For streaming across machines, use a message system such as Kafka or NATS, and keep the in-process pipeline for work inside each worker.
Size the pool to the slowest resource. CPU-bound work usually matches runtime.NumCPU(), while I/O-bound work can use more, limited by what the database or API accepts.
Pass a context.Context to every stage and cancel it. Each stage must select on ctx.Done() when it sends or receives.
Use channels to pass ownership of data between stages, and a mutex to protect shared state such as counters. Both are fine when used consistently.
Always close output channels from the sending side, and make every blocking send or receive cancellable through context.
Batch processing in Go comes down to chunking input, running a bounded pool of workers, passing data between stages with channels, and cancelling through context. Collect errors with errors.Join, make operations idempotent, and bring in external schedulers only when you need persistence or distribution.