Guess what? I ran your app and I get varying outputs: sometimes
50 (and sometimes
If you run your app with the race detector enabled (
go run -race play.go), it tells you have data races:
================== WARNING: DATA RACE Read at 0x00c00009a010 by goroutine 7: main.incr() /home/icza/gows/src/play/play.go:9 +0x3a Previous write at 0x00c00009a010 by goroutine 6: main.incr() /home/icza/gows/src/play/play.go:9 +0x50 Goroutine 7 (running) created at: main.main() /home/icza/gows/src/play/play.go:17 +0x83 Goroutine 6 (finished) created at: main.main() /home/icza/gows/src/play/play.go:17 +0x83 ==================
When you have data races, the behavior of your app is undefined. "Seemingly working sometimes" also fits into the "undefined" behavior, but undefined also means it can do anything else too.
See related questions: