Chapter 4. Go race detector

Go Toolset includes the Go race detector, which is a feature of the Go standard library.

4.1. Installing the race detector

On Red Hat Enterprise Linux 7, the race detector is provided by the go-toolset-1.15-golang-race package:

# yum install go-toolset-1.15-golang-race

On Red Hat Enterprise Linux 8, the race detector is provided by the golang-race package:

# yum install golang-race

This command installs a variant of the Go standard library that contains runtime race detection.

4.2. Using the race detector

To use the runtime race detector in a Go project, add the -race option to the go tool commands used when manipulating the project.

For a minimal approach to using the race detector, build the project with the -race option:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.15 'go build -race -o output_file go_main_package'
  • For Red Hat Enterprise Linux 8:

    $ go build -race -o output_file go_main_package

Run the resulting executable binary file, and the race detector prints warnings to the standard output when a race is detected.

Important

The race detector has a significant runtime resource overhead.

Note

You can execute any command using the scl utility on Red Hat Enterprise Linux 7, causing it to be run with the Go binaries available. To use Go Toolset on Red Hat Enterprise Linux 7 without a need to use scl enable with every command, run a shell session with:

$ scl enable go-toolset-1.15 'bash'

4.3. Additional resources

A detailed description of the Go race detector and its features is beyond the scope of this book. For more information, see the resources listed below.

Online documentation

See also

  • Chapter 1, Go — An overview of Go and more information on how to install it on your system.