title: Iterate over all the values a channel sends until it is closed in Go
body: <p>I am trying to understand how goroutines and channels work. I have a loop sending values to a channel and I'd like to iterate over all the values the channel sends until it is closed.</p>
  
  <p>I have written a simple example here:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
  )
  
  func pinger(c chan string) {
      for i := 0; i &lt; 3; i++ {
          c &lt;- "ping"
      }
      close(c)
  }
  
  func main() {
      var c chan string = make(chan string)
  
      go pinger(c)
  
      opened := true
      var msg string
  
      for opened {
          msg, opened = &lt;-c
          fmt.Println(msg)
      }
  }
  </code></pre>
  
  <p>This gives the expected result but I'd like to know if there is a shorter way of doing this.</p>
  
  <p>Many thanks for your help</p>