All PatternsConcurrency
Fan-Out/Fan-In
Distributes work across multiple goroutines and collects results.
Parallel API callsData processingMap-reduce
Understanding Fan-Out/Fan-In
Fan-Out/Fan-In distributes work across multiple goroutines (fan-out) and then merges results back into a single channel (fan-in). It's ideal for parallel processing of streams.
How It Works
Input
Stream
1
2
3
4
Worker 1
Worker 2
Worker 3
Merged Output
Results
1
4
9
16
Single input stream
1
Input Stream
Single stream of data to process.
1 / 4
Basic Implementation
Number squaring with fan-out/fan-in:
main.go
Loading editor...
Real-World Example: Document Processing
Parallel document analysis:
main.go
Loading editor...