title: breaking out of a select statement when all channels are closed
body: <p>I have two goroutines independently producing data, each sending it to a channel. In my main goroutine, I'd like to consume each of these outputs as they come in, but don't care the order in which they come in. Each channel will close itself when it has exhausted its output. While the select statement is the nicest syntax for consuming inputs independently like this, I haven't seen a concise way for looping over each of until both of the channels have closed.</p>
  
  <pre><code>for {
      select {
      case p, ok := &lt;-mins:
          if ok {
              fmt.Println("Min:", p) //consume output
          }
      case p, ok := &lt;-maxs:
          if ok {
              fmt.Println("Max:", p) //consume output
          }
      //default: //can't guarantee this won't happen while channels are open
      //    break //ideally I would leave the infinite loop
                  //only when both channels are done
      }
  }
  </code></pre>
  
  <p>the best I can think to do is the following (just sketched, may have compile errors):</p>
  
  <pre><code>for {
      minDone, maxDone := false, false
      select {
      case p, ok := &lt;-mins:
          if ok {
              fmt.Println("Min:", p) //consume output
          } else {
              minDone = true
          }
      case p, ok := &lt;-maxs:
          if ok {
              fmt.Println("Max:", p) //consume output
          } else {
              maxDone = true
          }
      }
      if (minDone &amp;&amp; maxDone) {break}
  }
  </code></pre>
  
  <p>But this looks like it would get untenable if you're working with more than two or three channels. The only other method I know of is to use a timout case in the switch statement, which will either be small enough to risk exiting early, or inject too much downtime into the final loop. Is there a better way to test for channels being within a select statement?</p>