I read this post about data races. And I understand, that access to interface variable need to be sync. But what about access to struct?
For example we have this code:
package main
import (
"fmt"
)
type s struct {
field0 uint8
field1 uint16
}
func main() {
s0 := s{
field0: 1,
field1: 2,
}
s1 := s{
field0: 3,
field1: 4,
}
var shared s
var loop0, loop1 func()
loop0 = func() {
shared = s0
go loop1()
}
loop1 = func() {
shared = s1
go loop0()
}
go loop0()
for {
fmt.Println(shared)
}
}
I build this code with -race
flag and race detector no detect any errors.
But if shared
is not a pointer, what really happened? We need set 2 fields (if on stack).
Questions:
- is it data race?
- if no, why?
UPD: start loop0