Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 15 of 60 · Go

Concurrency Patterns: Worker Pools

Duration: 25 minutes

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 chan<- int) {
    for j := range jobs {
        fmt.Printf("worker %d processing %d\n", id, j)
        results <- j * 2
    }
}

func main() {
    const numWorkers = 3
    jobs := make(chan int, 100)
    results := make(chan int, 100)

    for w := 1; w <= numWorkers; w++ {
        go worker(w, jobs, results)
    }

    for j := 1; j <= 5; j++ { jobs <- j }
    close(jobs)

    for a := 1; a <= 5; a++ { fmt.Println(<-results) }
}

Cancellation with context

ctx, cancel := context.WithCancel(context.Background())
go func() { time.Sleep(2 * time.Second); cancel() }()
select {
case <-ctx.Done(): fmt.Println("canceled")
case v := <-results: fmt.Println(v)
}

Info

Always prefer a bounded worker pool over spawning an unbounded number of goroutines – helps avoid OOM.

Previous: Channels: Communication Between GoroutinesNext: Atomic Operations & Mutexes