Concurrency PatternsIntermediate
1Question 1 of 1
goWhat are common Go Concurrency Patterns (Worker Pools, Fan-In/Fan-Out)?
Explanation & Concept
Common Go concurrency architectures: • Worker Pool: A fixed number of goroutines consuming tasks from a shared channel, bounding resource usage. • Fan-Out: Distributing multiple tasks across multiple goroutines to run in parallel. • Fan-In: Combining outputs from multiple channels into a single consolidated channel.
Code Example
go
package main
import "fmt"
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
results <- j * 2 // Process job
}
}
func main() {
const numJobs = 5
jobs := make(chan int, numJobs)
results := make(chan int, numJobs)
// Launch 3 concurrent workers in pool
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= numJobs; j++ {
jobs <- j
}
close(jobs)
for a := 1; a <= numJobs; a++ {
fmt.Println("Result:", <-results)
}
}How It Works & Key Takeaways
Worker pools prevent unbounded goroutine creation, keeping memory usage constant regardless of input task volume.
Mock Test · 1 Questions · Optional
Ready for Pointers?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions