title: What is an elegant way to shut down a chain of goroutines linked by channels?
body: <p>I'm a Go learner. In order better to understand the care and feeding of channels and goroutines, I'm trying to build a Sieve of Eratosthenes as a set of goroutines connected into a pipeline by channels.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>// esieve implements a Sieve of Eratosthenes
  // as a series of channels connected together
  // by goroutines
  package main
  
  import "fmt"
  
  func sieve(mine int, inch chan int) {
      start := true                        // First-number switch
      ouch := make(chan int)               // Output channel for this instance
      fmt.Printf("%v\n", mine)             // Print this instance's prime
      for next := &lt;-inch; next &gt; 0; next = &lt;-inch {  // Read input channel
          fmt.Printf("%v &lt;- %v\n",mine,next)         // (Trace)
          if (next % mine) &gt; 0 {                     // Divisible by my prime?
              if start {                   // No; is it the first number through? 
                  go sieve(next, ouch)     // First number - create instance for it
                  start = false            // First time done
              } else {                     // Not first time
                  ouch &lt;- next             // Pass it to the next instance
              }
          }
      }
  }
  
  func main() {
      lim := 30                     // Let's do up to 30
      fmt.Printf("%v\n", 2)         // Treat 2 as a special case
      ouch := make(chan int)        // Create the first segment of the pipe
      go sieve(3, ouch)             // Create the instance for '3'
      for prime := 3; prime &lt; lim; prime += 2 { // Generate 3, 5, ...
          fmt.Printf("Send %v\n", prime)        // Trace
          ouch &lt;- prime                         // Send it down the pipe
      }
  }
  </code></pre>
  
  <p>And as far as it goes, it works nicely.</p>
  
  <p>However, when I finish the main loop, <code>main</code> exits before all the numbers still in the pipeline of <code>sieve</code> instances have propagated down to the end.</p>
  
  <p>What is the simplest, most elegant, or generally accepted way to make a main routine wait for a set of goroutines (about which it only 'knows' of the first one) to complete?</p>