Race Detector and Safe Patterns
Using -race flag and avoiding data races
Race Detector and Safe Patterns is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a data race?
A data race occurs when two goroutines access the same memory location concurrently and at least one access is a write, without synchronisation. Data races cause undefined behaviour.
Enabling the race detector
Run or test with -race to enable Go's built-in race detector:
go run -race main.go
go test -race ./...Race detector output
When a race is detected, the detector prints goroutine stack traces showing the conflicting accesses and terminates the program. The output points to the exact lines involved.
Performance cost
The race detector adds ~5-10× memory overhead and ~2-20× slower execution. Run it in CI and test environments, not in production builds.
Common safe patterns: atomic
sync/atomic provides lock-free operations for simple integer counters and pointers, avoiding the overhead of a full mutex.
var counter int64
atomic.AddInt64(&counter, 1)
fmt.Println(atomic.LoadInt64(&counter))Common safe patterns: channel ownership
Only one goroutine owns a value at a time; ownership transfers through channels. No shared memory means no races.
ch := make(chan []byte)
go func() { ch <- makeData() }()
data := <-ch // sole owner nowCommon safe patterns: immutable data
Read-only data shared between goroutines is safe. Only write before goroutines start or synchronise writes. String and [N]T values are safely shared after creation.
Detecting races in tests
The race detector works with go test -race. Write tests that exercise concurrent paths — the detector catches races that benign sequential tests miss.
sync.atomic vs mutex
Use atomics for single-variable updates (counters, flags). Use mutexes for protecting invariants that span multiple variables. Mixing them without careful analysis is error-prone.
Happens-before
Go guarantees happens-before relationships for channel operations, mutex Lock/Unlock, and sync.Once.Do. Operations within the same goroutine are ordered sequentially.
False sharing
Two goroutines updating adjacent memory (different fields of the same cache line) can cause cache thrashing. Pad structs with [64]byte if profiling shows contention on a specific struct.
Quick Check
Which flag enables Go's race detector?
Recap: Race Detector and Safe Patterns
Key points:
- Always run go test -race in CI
- Safe patterns: channels, mutexes, atomics, immutable data
- Race detector output shows exact goroutine stacks
- Do not use -race in production builds
Frequently asked questions
Is the “Race Detector and Safe Patterns” lesson free?
Yes — the full text of “Race Detector and Safe Patterns” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Race Detector and Safe Patterns”?
Using -race flag and avoiding data races You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Race Detector and Safe Patterns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- sync.Mutex and sync.RWMutex
- sync.WaitGroup
- sync.Once and sync.Map
- Race Detector and Safe Patterns