01 / 10

Explain the fan-out and fan-in concurrency pattern with a practical example.

Fan-out distributes work across multiple goroutines reading from a shared input channel. Fan-in merges results from multiple output channels into a single channel for the consumer.

Fan-out and fan-in pipeline
Key design considerations
  1. 1

    Always close channels when producers are done — ranging goroutines exit cleanly

  2. 2

    Use context cancellation to stop all workers when one fails or timeout occurs

  3. 3

    Buffer the merged channel to avoid one slow worker blocking others

  4. 4

    WaitGroup in fan-in ensures merged channel is closed only after all workers finish

  5. 5

    For CPU-bound work, limit fan-out to runtime.NumCPU() workers