title: Can't figure out why this loop is a data race
body: <p>I have a loop that is apparently causing a data race its near the bottom of this function and I will have it marked:</p>
  
  <pre><code>func (p *PartialParty) SendReadyCheck(party PartialParty) {
      msg, err := json.Marshal(&amp;ReadyCheckMsg{"ReadyCheck", ""})
      if err != nil {
          log.Println(err)
      }
  
      for _, member := range party.Members {
          member.Conn.send &lt;- msg
      }
  
      counter := 0
      loopBreaker := true
      for {
          select {
          case &lt;-p.Accept:
              counter++
              resp, err := json.Marshal(&amp;ReadyCheckMsg{"ReadyAccepted", ""})
              if err != nil {
                  log.Println(err)
              }
              for _, member := range party.Members {
                  member.Conn.send &lt;- resp
              }
              if counter == 2 {
                  // Create a new party with all members
                  partyid := PartyID(feeds.NewUUID().String())
                  db := common.Db()
                  newParty := &amp;Party{
                      Active:    true,
                      Members:   p.Members,
                      Broadcast: make(chan []byte),
                      PartyID:   partyid,
                  }
  
                  // Insert the new party into the database
                  _, err := db.Exec("INSERT INTO party SET party_id = ?, active = ?", partyid.String(), true)
                  if err != nil {
                      log.Println(err)
                  }
  
                  // Go through the members and update the database
                  var wg sync.WaitGroup
  
                  for _, member := range party.Members {
                      wg.Add(1)
                      m := member
                      go func() {
                          _, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, m.SteamID)
                          if err != nil {
                              log.Println(err)
                          }
                          _, err = db.Exec("INSERT INTO party_members SET belongs_to =?, active = ?, steamid = ?", partyid.String(), true, m.SteamID)
                          if err != nil {
                              log.Println(err)
                          }
                          wg.Done()
                      }()
                  }
  
                  // Wait for all the database stuff to finish
                  wg.Wait()
                  PHub.AddNewParty(newParty)
                  loopBreaker = false
                  break
              }
          case conn := &lt;-p.Decline:
              if conn.Ready {
                  break
              }
              conn.Ready = false
              conn.InQueue = false
              conn.CurrentParty = ""
              resp, err := json.Marshal(&amp;ReadyCheckMsg{"ReadyCheckDeclined", ""})
              if err != nil {
                  log.Println(err)
              }
              p.Accepting = true
              identifier := conn.Identifier
              if _, ok := party.Members[identifier]; ok {
                  delete(p.Members, identifier)
              }
              for _, m := range party.Members {
                  member := m
                  member.Conn.send &lt;- resp
              }
              log.Println("Here")
              loopBreaker = false
              break
          case &lt;-time.After(30 * time.Second):
              if counter == 2 {
                  return
              }
              p.Accepting = true
              failedMsg, err := json.Marshal(&amp;ReadyCheckMsg{"FailedToReady", ""})
              if err != nil {
                  log.Println(err)
              }
              somebodyDeclinedMsg, err := json.Marshal(&amp;ReadyCheckMsg{"ReadyCheckDeclined", ""})
              if err != nil {
                  log.Println(err)
              }
  
  
      &gt;&gt;&gt;&gt;     for _, member := range party.Members { ***&lt;&lt;&lt;&lt; This Line***
                  m := member
                  if !m.Conn.Ready {
                      m.Conn.Ready = false
                      m.Conn.InQueue = false
                      m.Conn.CurrentParty = ""
                      m.Conn.send &lt;- failedMsg
                  } else {
                      m.Conn.Ready = false
                      m.Conn.send &lt;- somebodyDeclinedMsg
                  }
              }
              loopBreaker = false
              break
          }
          if !loopBreaker {
              break
          }
      }
  }
  </code></pre>
  
  <p>It is apparently conflicting with this:</p>
  
  <pre><code>// AddNewMember will add a new user to the party
  func (p *PartyHub) AddNewMember(member *Member, partyid PartyID) {
      p.Lock()
      defer p.Unlock()
  &gt;&gt;&gt; p.PartialPartys[partyid].Members[member.Conn.Identifier] = member
  }
  
  type PartialParty struct {
      Accepting bool
      Members   map[Identifier]*Member
      Accept    chan *Connection
      Decline   chan *Connection
      PartyID   PartyID
      sync.Mutex
  }
  </code></pre>
  
  <p>Right now it is impossible to <code>AddNewMember</code> if the goroutine <code>SendReadyCheck</code>  is running ``because it is protected by an if statement that checks if the goroutine is running, so I'm not sure why they are saying they are racing each other. Any help on clearing this up would be great. I've tried setting a variable inside the loop to try to get away from it but it doesn't seem to cause it</p>