Fan Out Fan In Pattern in Go: Explanation and Example
fan out fan in pattern in Go is a way to run multiple goroutines concurrently to process tasks (fan out) and then collect their results into a single channel (fan in). It helps efficiently handle parallel work and combine outputs using Go's channels and goroutines.How It Works
Imagine you have a big job that can be split into smaller tasks. Instead of doing them one by one, you ask several friends to work on different parts at the same time. This is the fan out part, where you start many goroutines to do tasks concurrently.
Once all your friends finish their parts, they send their results back to you. You collect all these results into one place. This is the fan in part, where multiple goroutines send their outputs into a single channel to be processed or combined.
In Go, this pattern uses channels to send tasks out and gather results back, making your program faster and more efficient by using concurrency.
Example
This example shows how to fan out work to multiple goroutines and fan in their results into one channel.
package main import ( "fmt" "time" ) func worker(id int, jobs <-chan int, results chan<- int) { for j := range jobs { fmt.Printf("Worker %d started job %d\n", id, j) time.Sleep(time.Second) // simulate work fmt.Printf("Worker %d finished job %d\n", id, j) results <- j * 2 } } func main() { jobs := make(chan int, 5) results := make(chan int, 5) // Fan out: start 3 workers for w := 1; w <= 3; w++ { go worker(w, jobs, results) } // Send 5 jobs for j := 1; j <= 5; j++ { jobs <- j } close(jobs) // Fan in: collect results for a := 1; a <= 5; a++ { result := <-results fmt.Println("Result received:", result) } }
When to Use
Use the fan out fan in pattern when you have many independent tasks that can run at the same time to speed up processing. For example:
- Processing multiple files or data streams concurrently.
- Making many network requests in parallel.
- Running CPU-bound tasks on multiple cores.
This pattern helps improve performance by using Go's lightweight goroutines and channels to manage concurrency cleanly.
Key Points
- Fan out means starting multiple goroutines to do work concurrently.
- Fan in means collecting results from many goroutines into one channel.
- This pattern uses channels to safely communicate between goroutines.
- It improves performance by parallelizing tasks.
- It is useful for IO-bound and CPU-bound concurrent workloads.