Are you familiar with patterns when working with channels and goroutines? Tell me about fan-in, fan-out, and others.
sobes.tech AI
Answer from AI
When working with channels and goroutines in Go, several patterns are often used to organize parallel data processing:
-
Fan-out — when multiple goroutines read from a single channel and process data in parallel. This increases throughput.
-
Fan-in — when multiple goroutines write to a single channel, merging results into one stream.
-
Worker pool — a group of goroutines (workers) that receive tasks from a channel and process them, which allows limiting the number of goroutines working simultaneously.
-
Pipeline — a sequence of processing stages, where each stage is a goroutine that reads from an input channel and writes to an output channel.
Example of fan-out and fan-in:
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Printf("worker %d processing 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 tasks
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
// fan-in: collect results
for a := 1; a <= 5; a++ {
fmt.Println("result", <-results)
}
}
These patterns help efficiently utilize parallelism and control the data flow between goroutines.