Fan-in and Fan-out Patterns
Merging multiple channels and distributing work
What is fan-out?
Fan-out distributes work from one channel across multiple goroutines, allowing parallel processing. Each worker receives from the same input channel.
Fan-out example
Spin up N workers all reading from a shared jobs channel:
func fanOut(jobs <-chan Job, n int) {
for i := 0; i < n; i++ {
go func() {
for job := range jobs {
process(job)
}
}()
}
}All lessons in this course
- The select Statement
- Timeouts and Tickers
- Fan-in and Fan-out Patterns
- Worker Pool Pattern