title: Solving goroutines deadlock
body: <p>I've been trying to solve this simple problem I encountered in Golang concurrency. I've been searching all possible solutions, but found nothing specific to my problem(or I might be missed one). Here's my code:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "time"
  )
  
  func producer(ch chan int, d time.Duration, num int) {
  
      for i:=0; i&lt;num; i++ {
          ch &lt;- i
          time.Sleep(d)
      }
  }
  
  func main() {
      ch := make(chan int)
  
      go producer(ch, 100*time.Millisecond, 2)
      go producer(ch, 200*time.Millisecond, 5)
  
      for {
          fmt.Println(&lt;-ch)    
      }
  
      close(ch)
  }
  </code></pre>
  
  <p>It prints error:</p>
  
  <blockquote>
    <p>fatal error: all goroutines are asleep - deadlock!</p>
    
    <p>goroutine 1 [chan receive]:
    main.main()
            D:/Code/go/src/testconcurrency/main.go:23 +0xca
    exit status 2</p>
  </blockquote>
  
  <p>What is the efficient way to avoid this error?, Thank you.</p>