package main

import (
	"fmt"
	"sync"
)

func worker(id int, jobs <-chan int, results chan<- int) {
	for job := range jobs {
		fmt.Printf("worker %d handled job %d\n", id, job)
		results <- job * job
	}
}

func main() {
	jobs := make(chan int)
	results := make(chan int)

	var workers sync.WaitGroup
	for id := 1; id <= 2; id++ {
		id := id
		workers.Go(func() {
			worker(id, jobs, results)
		})
	}

	go func() {
		for job := 1; job <= 4; job++ {
			jobs <- job
		}
		close(jobs)
	}()

	go func() {
		workers.Wait()
		close(results)
	}()

	for result := range results {
		fmt.Println("result:", result)
	}
}
