0PricingLogin
Go Academy · Lesson

Fan-Out Fan-In

Distribute and collect work.

Fan-Out and Fan-In

Fan-out means starting several goroutines to read from one channel, distributing work. Fan-in means merging the outputs of several goroutines back into one channel.

  • Fan-out spreads load across workers
  • Fan-in consolidates results for the consumer

A Generator Stage

The pipeline starts with a generator: a function that returns a channel and feeds values into it from its own goroutine, then closes it.

func gen(nums ...int) <-chan int {
    out := make(chan int)
    go func() {
        for _, n := range nums {
            out <- n
        }
        close(out)
    }()
    return out
}

All lessons in this course

  1. Worker Pool Pattern
  2. Fan-Out Fan-In
  3. Pipeline Stages
  4. Graceful Shutdown
← Back to Go Academy