~/Golang Race Detector Usage Guide

Jul 10, 2021


The Go race detector is a built-in tool that finds data races in concurrent Go programs.

To use the race detector, simply build or test your code with the -race flag. For example:

1
2
3
go run -race main.go
go test -race ./...
go build -race -o app

A data race occurs when two goroutines access the same variable concurrently, and at least one of the accesses is a write. Race conditions can cause unpredictable bugs that are difficult to reproduce.

The race detector increases binary size and reduces performance, so only use it during development or testing.

When a race is detected, the output shows the conflicting accesses, goroutine traces, and line numbers for rapid debugging.

For more, read the Go race detector documentation.

Tags: [golang] [concurrency] [tools]