You have goroutines reading the m
varaible, and goroutines writing the m
variable without explicit synchronization. This is a data race, and therefore undefined behaviour.
Run it with the race detector enabled:
$ go run -race play.go
==================
WARNING: DATA RACE
Write at 0x00c00008c000 by goroutine 15:
main.main.func2()
/home/icza/gows/src/play/play.go:17 +0x46
Previous read at 0x00c00008c000 by goroutine 5:
main.main.func1()
/home/icza/gows/src/play/play.go:8 +0x45
Goroutine 15 (running) created at:
main.main()
/home/icza/gows/src/play/play.go:15 +0xdd
Goroutine 5 (finished) created at:
main.main()
/home/icza/gows/src/play/play.go:7 +0xa4
==================
Found 1 data race(s)
exit status 66
See related questions:
Is it safe to read a function pointer concurrently without a lock?
Also an example which breaks Go's memory safety with intentional data race: Golang data races to break memory safety