title: Why do I receive values from a closed channel?
body: <p>I'm investigating the channels behaviour and I'm quite confused by their behaviour. The spec say <code>After calling close, and after any previously sent values have been received, receive operations will return the zero value for the channel's type without blocking.</code> However it seems I still get the values in the range statement even if by that time the channel is closed. Why is that?</p>
  
  <pre><code>package main
  
  import "fmt"
  import "sync"
  import "time"
  
  func main() {
      iCh := make(chan int, 99)
      var wg sync.WaitGroup
      go func() {
          for i := 0; i &lt; 5; i++ {
              wg.Add(1)
              go func(i int) {
                  defer wg.Done()
                  iCh &lt;- i
              }(i)
  
          }
          wg.Wait()
          close(iCh)
      }()
      time.Sleep(5 * time.Second)
      print("the channel should be closed by now\n")
      for i := range iCh {
          fmt.Printf("%v\n", i)
      }
      print("done")
  }
  </code></pre>
  
  <p><strong>Edit:</strong> It seems that if I move the <code>close</code> statement just before the channel range it closes it for good. So I'm wondering why it's not working with the "time.Sleep" trick too . By that time (5 seconds) all the go routines should have been completed and the channel closed, isn't it ?</p>