How do I implement a worker pool in Go?

Implementing a worker pool in Go is an efficient way to manage concurrent tasks, improving performance by limiting the number of goroutines that execute simultaneously. A worker pool helps in creating a balance between task submission and execution, ensuring system resources are utilized effectively.

keywords: Go, worker pool, concurrency, goroutines, task management, performance optimization
description: Learn how to implement a worker pool in Go to manage concurrent tasks efficiently and make the best use of system resources.
        
package main

import (
    "fmt"
    "sync"
)

type Job struct {
    ID int
}

func worker(jobs <-chan Job, wg *sync.WaitGroup) {
    defer wg.Done()
    for job := range jobs {
        fmt.Printf("Processing job ID: %d\n", job.ID)
    }
}

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

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

    // Send jobs to the workers
    for i := 1; i <= 10; i++ {
        jobs <- Job{ID: i}
    }
    
    close(jobs) // Close the jobs channel
    wg.Wait()   // Wait for all workers to finish
}
        
    

keywords: Go worker pool concurrency goroutines task management performance optimization