How to Use the Go Race Detector (-race Flag)

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 ./...