flâneur — a map of the web's best reading

Data Race Detector - The Go Programming Language

go.dev · 1,458 words · saved by 1 readers

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