package main

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

// section: fixed
func processWithWorkers(items []string, workers int) {
	ch := make(chan string)
	var wg sync.WaitGroup

	for i := 0; i < workers; i++ {
		wg.Add(1)
		go func(id int) {
			defer wg.Done()
			for item := range ch {
				fmt.Printf("Worker %d processing: %s\n", id, item)
				time.Sleep(50 * time.Millisecond)
			}
			fmt.Printf("Worker %d exiting\n", id) // NOW PRINTED
		}(i)
	}

	for _, item := range items {
		ch <- item
	}
	close(ch) // Signal workers to exit
	wg.Wait() // Wait for all workers to finish
}

// section: fixed

func main() {
	items := []string{"a", "b", "c", "d", "e"}
	processWithWorkers(items, 3)

	fmt.Println("All workers exited cleanly - no leaks")
}
