Simple worker pool in Go
<p>I am trying to implement a simple worker pool in go and keep running into issues. All I want to do is have a set number of workers that do a set amount of work before getting more work to do. The code I am using looks similar to:</p>

<pre><code>    jobs := make(chan imageMessage, 1)
    results := make(chan imageMessage, 1)

    for w := 0; w &lt; 2; w++ {
        go worker(jobs, results)
    }

    for j := 0; j &lt; len(images); j++ {
        jobs &lt;- imageMessage{path: paths[j], img: images[j]}
    }
    close(jobs)

    for r := 0; r &lt; len(images); r++ {
        &lt;-results
    }
}

func worker(jobs &lt;-chan imageMessage, results chan&lt;- imageMessage) {
    for j := range jobs {
        processImage(j.path, j.img)
        results &lt;- j
    }
}
</code></pre>

<p>My understanding is that this should create 2 workers that can do 1 "thing" at a time and will continue to get more work as they complete that 1 thing until there is nothing else to do. However, I get <code>fatal error: all goroutines are asleep - deadlock!</code></p>

<p>If I set the buffer to something huge like 100, this works, but I want to be able to limit the work done at a time.</p>

<p>I feel like I'm close, but obviously missing something.</p>