title: Need help understanding why select{} isn't blocking forever
body: <p>I am working on an exercise in using channels to implement a queue. Specifically, I am trying to use the size of a channel to limit the number of simultaneous goroutines. To wit, I have written the following code:</p>
  
  <pre><code>package main
  
  import "fmt"
  import "time"
  import "math/rand"
  
  func runTask (t string, ch *chan bool) {
          start := time.Now()
          fmt.Println("starting task", t)
          time.Sleep(time.Millisecond * time.Duration(rand.Int31n(1500))) // fake processing time
          fmt.Println("done running task", t, "in", time.Since(start))
          &lt;- *ch
  }
  
  func main() {
          numWorkers := 3
          files := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
  
          activeWorkers := make(chan bool, numWorkers)
  
          for _, f := range files {
                  activeWorkers &lt;- true
                  fmt.Printf("activeWorkers is %d long.\n", len(activeWorkers))
                  go runTask(f, &amp;activeWorkers)
          }
          select{}
  }
  </code></pre>
  
  <p>Right now, the code crashes with:</p>
  
  <pre><code>throw: all goroutines are asleep - deadlock!
  </code></pre>
  
  <p>My expectation was that the call to select would block forever and let the goroutines terminate without a deadlock.</p>
  
  <p>So I have a two-fold question: why isn't select blocking forever and, short of throwing in a time.Sleep() call after the for loop, how can I avoid deadlocks?</p>
  
  <p>Cheers,</p>
  
  <p>-mtw</p>