Data Race Detector - The Go Programming Language
Data races are among the most common and hardest to debug types of bugs in concurrent systems. A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write. See the The Go Memory Model for details. Here is an example of a data race that can lead to crashes and memory corruption: To help diagnose such bugs, Go includes a built-in data race detector. To use it, add the -race flag to the go command: When the race detector finds a data race in the program, it prints a report. The report contains stack traces for conflicting accesses, as well as stacks where the involved goroutines were created. Here is an example: The GORACE environment variable sets race detector options. The format is: The options are: Example: When you build with -race flag, the go command defines additional build tag race. You can use the tag to exclude some code and tests when running the race detector. Some examples: To start, run your tests using the race d
Data Race Detector Introduction Data races are among the most common and hardest to debug types of bugs in concurrent systems. A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write. See the The Go Memory Model for details. Here is an example of a data race that can lead to crashes and memory corruption: func main() { c := make(chan bool) m := make(map[string]string) go func() { m["1"] = "a" // First conflicting access. c <- true }() m["2"] = "b" // Second conflicting access. <-c for k, v := range m { fmt.Println(k, v) } } Usage
Explore this link on the map →saved by
related reading
- The Go Memory Model - The Go Programming Languagego.dev
- Frequently Asked Questions (FAQ) - The Go Programming Languagego.dev
- Reading 16: Mutual Exclusionweb.mit.edu
- Problem Set 4: Memory Scrambleweb.mit.edu
- golang mutex, go mutex, mutex lock golang, golang mutex example, sync.RWMutexgolinuxcloud.com
- Notes on structured concurrency, or: Go statement considered harmful - njs blogvorpus.org
- Go go-to guide · YourBasicyourbasic.org
- Diagnostics - The Go Programming Languagego.dev
- sync package - sync - Go Packagespkg.go.dev
- Writing Better Go: Lessons from 10 Code Reviews - Speaker Deckspeakerdeck.com
- Writing Better Go: Lessons from 10 Code Reviews - Speaker Deckspeakerdeck.com
- Go execution tracer | Gopher Academy Blogblog.gopheracademy.com