Skip to content

Go Channel Fan-Out Pattern — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Go Channel Fan. We cover key concepts, practical examples, and best practices.

Channel fan-out -- Distribute work to multiple worker goroutines by having them all read from the same input channel.

The Problem

Fan-out splits a stream of data across multiple goroutines for parallel processing. Each value from the input channel goes to exactly one worker (not all).

Wrong

data := []int{1, 2, 3, 4, 5}
for _, d := range data {
    go process(d) // Fan-out but no channel coordination
}

Output:

// Works but no backpressure or rate limiting
jobs := make(chan int, 100)
var wg sync.WaitGroup
// Start 3 workers
for i := 0; i < 3; i++ {
    wg.Add(1)
    go func(id int) {
        defer wg.Done()
        for job := range jobs {
            fmt.Printf("Worker %d processing %d
", id, job)
        }
    }(i)
}
// Send jobs
for i := 1; i <= 5; i++ {
    jobs <- i
}
close(jobs)
wg.Wait()

Output:

Worker 2 processing 1
Worker 0 processing 2
Worker 1 processing 3
Worker 0 processing 4
Worker 2 processing 5

Prevention

  • Multiple goroutines reading same channel = fan-out
  • Each value goes to exactly one worker
  • Close channel to signal no more work
  • Use WaitGroup to wait for all workers
  • Buffered channel for smooth distribution

Common Mistakes with channel fan out

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

**Does fan-out guarantee ordering?**

No. Workers may process in any order.

Can workers send results back?

Yes. Use a result channel (fan-in) for outputs.

How many workers?

Based on workload. CPU-bound = GOMAXPROCS. IO-bound = more.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro