title: Why goroutine leaks
body: <p>I read <a href="https://talks.golang.org/2013/bestpractices.slide#30" rel="nofollow">Twelve Go Best Practices</a> and encounter and interesting example on page 30.</p>
  
  <pre><code>func sendMsg(msg, addr string) error {
      conn, err := net.Dial("tcp", addr)
      if err != nil {
          return err
      }
      defer conn.Close()
      _, err = fmt.Fprint(conn, msg)
      return err
  } 
  
  func broadcastMsg(msg string, addrs []string) error {
      errc := make(chan error)
      for _, addr := range addrs {
          go func(addr string) {
              errc &lt;- sendMsg(msg, addr)
              fmt.Println("done")
          }(addr)
      }
  
      for _ = range addrs {
          if err := &lt;-errc; err != nil {
              return err
          }
      }
      return nil
  }
  
  func main() {
      addr := []string{"localhost:8080", "http://google.com"}
      err := broadcastMsg("hi", addr)
  
      time.Sleep(time.Second)
  
      if err != nil {
          fmt.Println(err)
          return
      }
      fmt.Println("everything went fine")
  }
  </code></pre>
  
  <p>The programmer mentioned, that happens to the code above:</p>
  
  <pre><code>the goroutine is blocked on the chan write
  the goroutine holds a reference to the chan
  the chan will never be garbage collected
  </code></pre>
  
  <p>Why the goroutine is blocked here? The main thread is blocked, until it receive data from goroutine. After it continues the for loop. Not?</p>
  
  <p>Why the errc chan will be never garbage collected? Because I do not close the channel, after goroutine is finished?  </p>