title: Go Channels and delays
body: <p>I was just experimenting with Go channels on my Ubuntu 64 bit environment and got confused with the output the following program produced.</p>
  
  <p>I got the output:
  0
  1
  2
  3
  Exit</p>
  
  <p>The output when I uncommented the two commented lines:
  0
  1
  2
  3
  4
  Exit</p>
  
  <p>Please explain the behavior.
  TIA.</p>
  
  <pre><code>package main
  
  import (
  "fmt"
      //"time"
  )
  
  func main() {
      ch := make(chan int)
      done := make(chan bool)
      go func() {
          for i := 0; i &lt; 5; i++ {
              ch &lt;- i
          }
                  //time.Sleep(1 * time.Second)
          done &lt;- false
      }()
      go func() {
          for {
              select {
              case message := &lt;-ch:
                  fmt.Println(message)
              case &lt;-done:
                  return
              }
          }
      }()
      &lt;-done
      fmt.Println("Exit")
  }
  </code></pre>