I have a loop and I get this error, I'm running go run -race main.go
This is the loop in question:
var wg sync.WaitGroup
for _, member := range p.Members { << Line 254
go func() {
wg.Add(1)
_, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, member.SteamID) << Line 257
if err != nil {
log.Println(err)
}
_, err = db.Exec("INSERT INTO party_members SET belongs_to =?, active = ?, steamid = ?", partyUUID, true, member.SteamID)
if err != nil {
log.Println(err)
}
wg.Done()
}() << Line 266
}
and then this is the error I get :
WARNING: DATA RACE
Read by goroutine 44:
BLAH/controllers/partybot.func·001()
/Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:257 +0x136
Previous write by goroutine 43:
BLAH/controllers/partybot.(*PartialParty).SendReadyCheck()
/Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:254 +0xda5
Goroutine 44 (running) created at:
BLAH/controllers/partybot.(*PartialParty).SendReadyCheck()
/Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:266 +0xf13
Goroutine 43 (finished) created at:
BLAH/controllers/partybot.(*PartyHub).SortMemberIntoParty()
/Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:173 +0xdb9
BLAH/controllers/partybot.(*Connection).readPump()
/Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:359 +0x1872
BLAH/controllers/partybot.WSPartyBot()
/Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:440 +0x4f0
net/http.HandlerFunc.ServeHTTP()
/usr/local/go/src/net/http/server.go:1265 +0x4e
github.com/gorilla/mux.(*Router).ServeHTTP()
/Users/Dev/gocode/src/github.com/gorilla/mux/mux.go:98 +0x377
BLAH/controllers/common.func·001()
/Users/Dev/gocode/src/BLAH/controllers/common/common.go:36 +0xc2
net/http.HandlerFunc.ServeHTTP()
/usr/local/go/src/net/http/server.go:1265 +0x4e
net/http.(*ServeMux).ServeHTTP()
/usr/local/go/src/net/http/server.go:1541 +0x20c
net/http.serverHandler.ServeHTTP()
/usr/local/go/src/net/http/server.go:1703 +0x1f6
net/http.(*conn).serve()
/usr/local/go/src/net/http/server.go:1204 +0x1087
==================
I'm not sure why this is a Data race as I'm fairly new to Go anyways if anybody could tell me why this is that would be great, I marked the lines in question.