package main
import "fmt"
var quit chan int
var glo int
func test() {
fmt.Println(glo)
}
func main() {
glo = 0
n := 10000
quit = make(chan int, n)
go test()
for {
quit <- 1
glo++
}
}
Situation:
The above program outputs 10000. But when I assign a bigger number to n (e.g. n := 1000000
), the output will be a random number less than n.
I haven't called runtime.GOMAXPROCS()
, so these two goroutines can't run in parallel. Executing go run -race
to detect race conditions, ends up without any warnings.
Question:
Why does this race condition occur?