title: Always have x number of goroutines running at any time
body: <p>I see lots of tutorials and examples on how to make Go wait for x number of goroutines to finish, but what I'm trying to do is have ensure there are always x number running, so a new goroutine is launched as soon as one ends.</p>
  
  <p>Specifically I have a few hundred thousand 'things to do' which is processing some stuff that is coming out of MySQL. So it works like this:</p>
  
  <pre><code>db, err := sql.Open("mysql", connection_string)
  checkErr(err)
  defer db.Close()
  
  rows,err := db.Query(`SELECT id FROM table`)
  checkErr(err)
  defer rows.Close()
  
  var id uint
  for rows.Next() {
      err := rows.Scan(&amp;id)
      checkErr(err)
      go processTheThing(id)
      }
  checkErr(err)
  rows.Close()
  </code></pre>
  
  <p>Currently that will launch several hundred thousand threads of <code>processTheThing()</code>. What I need is that a maximum of x number (we'll call it 20) goroutines are launched. So it starts by launching 20 for the first 20 rows, and from then on it will launch a new goroutine for the next id the moment that one of the current goroutines has finished. So at any point in time there are always 20 running.</p>
  
  <p>I'm sure this is quite simple/standard, but I can't seem to find a good explanation on any of the tutorials or examples or how this is done.</p>