title: Channels and Wait Groups Entering Deadlock
body: <p>I'm having trouble wrangling go routines and getting them to communicate back to a channel on the main go routine. To simplify, my code looks something like this:</p>
  <pre class="lang-golang prettyprint-override"><code>
  func main() {
      channel := make(chan string)
      var wg sync.WaitGroup
      for i := 0; i &lt; 10; i++ {
          wg.Add(1)
          go performTest(channel, &amp;wg, i)
      }
  
      wg.Wait()
      close(channel)
  
      for line := range channel {
          fmt.Print(line)
      }
  }
  
  func performTest(channel chan string, wg *sync.WaitGroup, i int) {
       defer wg.Done()
       // perform some work here
       result := fmt.sprintf(&quot;Pretend result %d&quot;, i)
       channel &lt;- result
  }
  </code></pre>
  <p>This seems to enter into some kind of a deadlock, but I don't understand why. It gets stuck on <code>wg.Wait()</code>, even though I would expect it to continue once all the goroutines have called <code>Done</code> on the wait group. What am I missing here? I'd like to wait for the goroutines, and then iterate over all results in the channel.</p>