Lesson 39 of 60
GoTesting Concurrency – Race Detector and `testing`
20 minutes
Course progress65%
Testing Concurrent Code
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
Race Detector
Run tests with -race to catch data races.
$ go test -race ./...
Example: Concurrent Counter
type Counter struct { n int; mu sync.Mutex }
Inc() { c.mu.Lock(); c.n++; c.mu.Unlock() }
Value() { c.mu.Lock(); c.mu.Unlock(); c.n }