Golang / Golang Internals and Memory Management Interview Questions
How do you implement a worker pool in Go?
A worker pool limits the number of goroutines working concurrently, preventing resource exhaustion when processing a large number of tasks. It is one of the most common Go concurrency patterns.
// Classic worker pool pattern
func workerPool(ctx context.Context, jobs <-chan Job, results chan<- Result, numWorkers int) {
var wg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return // cancelled
case job, ok := <-jobs:
if !ok { return } // channel closed — no more jobs
result := process(job)
select {
case results <- result:
case <-ctx.Done(): return
}
}
}
}(i)
}
// Close results after all workers finish
go func() {
wg.Wait()
close(results)
}()
}
// Usage
const numWorkers = 10
jobs := make(chan Job, 100)
results := make(chan Result, 100)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
workerPool(ctx, jobs, results, numWorkers)
// Feed jobs
go func() {
defer close(jobs)
for _, job := range allJobs {
select {
case jobs <- job:
case <-ctx.Done(): return
}
}
}()
// Collect results
for r := range results {
fmt.Println(r)
}The worker pool pattern ensures bounded concurrency — with virtual threads in other languages this is less critical, but in Go it matters when each goroutine holds OS resources (database connections, file handles) that are finite. Set numWorkers based on the resource constraint: for I/O-bound work constrained by a connection pool of size N, use N workers. For CPU-bound work, use runtime.NumCPU() workers.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
