title: Will this code with unbuffered channel cause goroutine leak in Go?
body: <p>I'm writing some golang concurrency codes with goroutines and channels, suspecting that my code may cause goroutine leaks. My situation is similar to the following code, or you can open this <a href="https://play.golang.org/p/tTlOzjAHmiy" rel="nofollow noreferrer">go playground link</a>.</p>
  
  <pre class="lang-golang prettyprint-override"><code>func main() {
      numCount := 3
      numChan := make(chan int)
  
      for i := 0; i &lt; numCount; i++ {
          go func(num int) {
              fmt.Printf("Adding num: %d to chan\n", num)
              numChan &lt;- num
              fmt.Printf("Adding num: %d to chan Done\n", num)
          }(i)
      }
  
      time.Sleep(time.Second)
      panic("Goroutine Resource Leak Test")
  } 
  </code></pre>
  
  <p>In my opinion, when the main goroutine returns, three goroutines are blocked sending to the unbuffered channel and there will be goroutine leak. This post <a href="https://stackoverflow.com/questions/46141353/goroutine-leak-with-buffered-channel-in-go">goroutine leak with buffered channel in Go</a> also suggests that <code>So only if the channel was unbuffered the leak would occur</code>. </p>
  
  <p><em>The Go Programming Language</em> suggests that: </p>
  
  <blockquote>
    <p>There’s a handy trick we can use during testing: if instead of returning from main in the event of cancellation, we execute a call to panic, then the runtime will dump the stack of every goroutine in the program. If the main goroutine is the only one left, then it has cleaned up after itself. But if other goroutines remain, they may not have been properly canceled, or perhaps they have been canceled but the cancellation takes time; a little investigation may be worthwhile. The panic dump often contains sufﬁcient information to distinguish these cases.</p>
  </blockquote>
  
  <p>Therefore, I added <code>panic("Goroutine Resource Leak Test")</code> to the end of the main function to verify my assumption. However, the panic dump only contains main goroutine, that is, there is no resource leak.</p>
  
  <pre><code>Adding num: 0 to chan
  Adding num: 1 to chan
  Adding num: 2 to chan
  panic: Goroutine Resource Leak Test
  
  goroutine 1 [running]:
  main.main()
      /tmp/sandbox011109649/prog.go:21 +0xc0
  </code></pre>
  
  <p>Can someone help explain</p>
  
  <ul>
  <li>why there is no goroutine leak, or</li>
  <li>how should I get the correct panic dump if there is leak</li>
  </ul>
  
  <p>Any suggestion will be appreciated, thanks in advance!</p>