How to Create a Worker Pool in Go with Channels

Create a Go worker pool by spawning goroutines that consume tasks from a buffered channel and wait for completion with sync.WaitGroup.

Create a worker pool by launching goroutines that pull tasks from a buffered channel and process them until the channel closes.

package main

import (
	"fmt"
	"sync"
	"time"
)

func worker(id int, jobs <-chan int, wg *sync.WaitGroup) {
	defer wg.Done()
	for j := range jobs {
		fmt.Printf("Worker %d started job %d\n", id, j)
		time.Sleep(time.Second) // Simulate work
		fmt.Printf("Worker %d finished job %d\n", id, j)
	}
}

func main() {
	const numWorkers = 3
	jobs := make(chan int, 100)
	var wg sync.WaitGroup

	for w := 1; w <= numWorkers; w++ {
		wg.Add(1)
		go worker(w, jobs, &wg)
	}

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