According to the docs doing the following prevents a data race:
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go func(j int) {
fmt.Println(j) // Good. Read local copy of the loop counter.
wg.Done()
}(i)
}
wg.Wait()
however if I have code such as:
type Job struct {
Work []string
}
var Queue chan Job
func work(id string, type int32) {
job := Job{Work: []string{"A","B","C"}}
go func(j Job, ty int32) {
if ty == 1 {
// do some other task
}
Queue <- j
} (job, type) // RACE ON THIS LINE
if type == 1 {
// do something
}
return
}
work is being called via. gRPC.
the data race detector tells me that the go routine is producing a data race. I am thinking that there is something I am missing here but can't quite seem to find what that is.