title: Synchronizing go routines in a round-based game
body: <p>I am totally new to Go and I am trying to code a game as an exercise.</p>
  <p>Basically the main flux will create <code>totalNumberOfPlayers</code> routines, each of them will perform some tasks following a round-based game. At the end of each round a player is removed from the game until only one player remains.</p>
  <p>In order to make sure the routines are synchronized in the same round I am trying to use a <code>WaitGroup</code> as follows:</p>
  <pre><code>func main() {
      // ... initialization code
  
      var wg sync.WaitGroup
      for i := 0; i &lt; totalNumberOfPlayers; i++ {
          go routineJob(i, ..., &amp;wg)
      }
      // TODO Temporary workaround to let the main flux wait
      fmt.Scanln()
  }
  </code></pre>
  <p>Each routine runs the same job:</p>
  <pre><code>func routineJob(routineId int, ..., wg *sync.WaitGroup) {
      // ... 
      for round := 0; round &lt; totalNumberOfPlayers-1; round++ {
          numberOfPlayers := totalNumberOfPlayers - round
          wg.Add(1) // Add player to round
          // ... perform some tasks
          if ... { // Player verifies condition to be removed from the game
              wg.Done() // Remove player from round
              return
          } 
          // ... perform some other tasks
          wg.Done() // Remove player from round
          wg.Wait() // Wait for all players to be removed before going to the next round
      }
  }
  </code></pre>
  <p>But at the end of the first round I get the following error:</p>
  <blockquote>
  <p>panic: sync: WaitGroup is reused before previous Wait has returned</p>
  </blockquote>
  <p>Looking around online I figured out that probably the problem is that after calling <code>wg.Wait()</code> I am not allowed to call <code>wg.Add(1)</code>. If that is the case, <strong>how can I implement a round based game</strong> like this?</p>
  <p><strong>Note:</strong> I was thinking to create an external <code>WaitGroup</code> for each round, but that would require me to create another loop somehow in the <code>main()</code> function and I would prefer to avoid this to keep the structure of the code mostly as presented.</p>