Lesson 15 of 60
GoConcurrency Patterns: Worker Pools
25 minutes
Course progress25%
Worker Pool Pattern
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
A common pattern for processing many jobs with a limited number of goroutines.
func worker(id int, jobs <-chan int, results <- ) {
j := jobs {
fmt.Printf(, id, j)
results <- j *
}
}
{
numWorkers =
jobs := ( , )
results := ( , )
w := ; w <= numWorkers; w++ {
worker(w, jobs, results)
}
j := ; j <= ; j++ { jobs <- j }
(jobs)
a := ; a <= ; a++ { fmt.Println(<-results) }
}