Use the -race flag with go run or go test to detect data races in your Go program.
Run your Go code with the -race flag to detect data races at runtime. This flag enables the Go race detector, which instruments your code to find concurrent access to the same memory location where at least one access is a write.
go run -race main.go
For tests, use:
go test -race ./...
The race detector acts like a security guard watching your code for dangerous traffic jams. It spots when two parts of your program try to change the same piece of data at the exact same time, which causes unpredictable crashes. You use it during development to catch these hidden bugs before they break your app in production.