title: Golang : Getting "fatal error: all goroutines are asleep - deadlock" on waitGroup.Wait()
body: <p>I am trying to write a code which does concurrent reads on a file and posts contents to one channel.</p>
  
  <p><a href="https://github.com/praveenk007/go-conc-fileReader/blob/master/main.go" rel="nofollow noreferrer">Here</a> is the link to my code, and the code:</p>
  
  <pre><code>func main() {
      bufferSize := int64(10)
      f, err := os.Open("tags-c.csv")
      if err != nil {
          panic(err)
      }
      fileinfo, err := f.Stat()
      if err != nil {
          fmt.Println(err)
          return
      }
      filesize := int64(fileinfo.Size())
      fmt.Println(filesize)
      routines := filesize / bufferSize
      if remainder := filesize % bufferSize; remainder != 0 {
          routines++
      }
      fmt.Println("Total routines : ", routines)
  
      channel := make(chan string, 10)
      wg := &amp;sync.WaitGroup{}
  
      for i := int64(0); i &lt; int64(routines); i++ {
          wg.Add(1)
          go read(i*bufferSize, f, channel, bufferSize, filesize, wg)
  
      }
      fmt.Println("waiting")
      wg.Wait()
      fmt.Println("wait over")
      close(channel)
  
      readChannel(channel)
  }
  
  func readChannel(channel chan string) {
      for {
          data, more := &lt;-channel
          if more == false {
              break
          }
          fmt.Print(data)
      }
  }
  
  func read(seek int64, file *os.File, channel chan string, bufferSize int64, filesize int64, wg *sync.WaitGroup) {
      defer wg.Done()
      fmt.Println("read :: ", seek)
      var buf []byte
      if filesize &lt; bufferSize {
          buf = make([]byte, filesize)
      } else if (filesize - seek) &lt; bufferSize {
          buf = make([]byte, filesize-seek)
      } else {
          buf = make([]byte, bufferSize)
      }
  
      n, err := file.ReadAt(buf, seek)
      if err != nil {
          log.Printf("loc %d err: %v", seek, err)
          return
      }
      if n &gt; 0 {
          channel &lt;- string(buf[:n])
          fmt.Println("ret :: ", seek)
      }
  }
  </code></pre>
  
  <p>I tried checking online but to my amaze I had already taken care of the solutions mentioned. Any help will be appreciated.</p>