How to Implement a Background Job Worker in Go

Implement a Go background job worker by spawning goroutines that consume tasks from a channel and send results back to the main program.

Use goroutines to process jobs from a channel concurrently, allowing the main program to continue while workers handle tasks in the background.

func worker(jobs <-chan int, results chan<- int) {
    for j := range jobs {
        results <- j * 2
    }
}

func main() {
    jobs := make(chan int, 100)
    results := make(chan int, 100)
    for w := 1; w <= 3; w++ {
        go worker(jobs, results)
    }
    go func() {
        for j := 1; j <= 10; j++ {
            jobs <- j
        }
        close(jobs)
    }()
    for a := 1; a <= 10; a++ {
        <-results
    }
}