title: Go channel infinite loop
body: <p>I am trying to catch errors from a group of goroutines using a channel, but the channel enters an infinite loop, starts consuming CPU.</p>
  
  <pre><code>func UnzipFile(f *bytes.Buffer, location string) error {
      zipReader, err := zip.NewReader(bytes.NewReader(f.Bytes()), int64(f.Len()))
  
      if err != nil {
          return err
      }
  
      if err := os.MkdirAll(location, os.ModePerm); err != nil {
          return err
      }
  
      errorChannel := make(chan error)
      errorList := []error{}
  
      go errorChannelWatch(errorChannel, errorList)
  
      fileWaitGroup := &amp;sync.WaitGroup{}
  
      for _, file := range zipReader.File {
          fileWaitGroup.Add(1)
          go writeZipFileToLocal(file, location, errorChannel, fileWaitGroup)
      }
  
      fileWaitGroup.Wait()
  
      close(errorChannel)
  
      log.Println(errorList)
  
      return nil
  }
  
  func errorChannelWatch(ch chan error, list []error) {
      for {
          select {
          case err := &lt;- ch:
  
              list = append(list, err)
          }
      }
  }
  
  func writeZipFileToLocal(file *zip.File, location string, ch chan error, wg *sync.WaitGroup) {
      defer wg.Done()
  
      zipFilehandle, err := file.Open()
  
      if err != nil {
          ch &lt;- err
          return
      }
  
      defer zipFilehandle.Close()
  
      if file.FileInfo().IsDir() {
          if err := os.MkdirAll(filepath.Join(location, file.Name), os.ModePerm); err != nil {
              ch &lt;- err
          }
          return
      }
  
      localFileHandle, err := os.OpenFile(filepath.Join(location, file.Name), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
  
      if err != nil {
          ch &lt;- err
          return
      }
  
      defer localFileHandle.Close()
  
      if _, err := io.Copy(localFileHandle, zipFilehandle); err != nil {
          ch &lt;- err
          return
      }
  
      ch &lt;- fmt.Errorf("Test error")
  }
  </code></pre>
  
  <p>So I am looping a slice of files and writing them to my disk, when there is an error I report back to the <code>errorChannel</code> to save that error into a slice.</p>
  
  <p>I use a <code>sync.WaitGroup</code> to wait for all goroutines and when they are done I want to print <code>errorList</code> and check if there was any error during the execution.</p>
  
  <p>The list is always empty, even if I add <code>ch &lt;- fmt.Errorf("test")</code> at the end of <code>writeZipFileToLocal</code> and the channel always hangs up.</p>
  
  <p>I am not sure what I am missing here.</p>