The Go race detector
In Chapter 8, Testing Microservice Architectures, we explored how to use the pprof
tool to profile the CPU and memory usage of Go applications. One of the essential tools that can help us find issues with concurrency is the Go race detector. It is a powerful tool that analyzes our code to find concurrency problems when an application is running.
Go’s race detector was added to the toolchain in Go 1.1, released in 2012. This tool was designed to help developers find race conditions in their code. As we have seen in the previous examples, writing concurrent code in Go is easy, but bugs can appear in even the most readable and well-designed code.
The race detector is enabled using the –race
command-line flag, alongside the go
command. For example, we can instruct it to run alongside our program:
$ go run –race main.go
The race detector can be used with other commands as well, including the build
and test
commands. This makes it easy...