title: Goroutines, Channels and Deadlock
body: <p>I'm trying to understand more about go's channels and goroutines, so I decided to make a little program that count words from a file, read by a <code>bufio.NewScanner</code> object:</p>
  
  <pre><code>nCPUs := flag.Int("cpu", 2, "number of CPUs to use")
  flag.Parse()
  runtime.GOMAXPROCS(*nCPUs)    
  
  scanner := bufio.NewScanner(file)
  lines := make(chan string)
  results := make(chan int)
  
  for i := 0; i &lt; *nCPUs; i++ {
      go func() {
          for line := range lines {
              fmt.Printf("%s\n", line)
              results &lt;- len(strings.Split(line, " "))
          }
      }()
  }
  
  for scanner.Scan(){
      lines &lt;- scanner.Text()
  }
  close(lines)
  
  
  acc := 0
  for i := range results {
        acc += i
   }
  
  fmt.Printf("%d\n", acc)
  </code></pre>
  
  <p>Now, in most examples I've found so far both the <code>lines</code> and <code>results</code> channels would be buffered, such as <code>make(chan int, NUMBER_OF_LINES_IN_FILE)</code>. Still, after running this code, my program exists with a <code>fatal error: all goroutines are asleep - deadlock!</code> error message.</p>
  
  <p>Basically my thought it's that I need two channels: one to communicate to the goroutine the lines from the file (as it can be of any size, I don't like to think that I need to inform the size in the <code>make(chan)</code> function call. The other channel would collect the results from the goroutine and in the main function I would use it to e.g. calculate an accumulated result. </p>
  
  <p>What should be the best option to program in this manner with goroutines and channels? Any help is much appreciated.</p>