How can I check if a race detector is enabled at runtime?

Is there a way to check if a Go program is compiled with -race

at runtime (for example, for logging / informational purposes)?

I've checked the documentation as well as the obvious locations ( runtime/*

) but I can't find anything.

+3


source to share


1 answer


There is no simple check for this as far as I can find, but when the -race

tag is on race

, so you can take advantage of that.

I created a new directory israce

and placed two files there:

israce/race.go

:

// +build race

// Package israce reports if the Go race detector is enabled.
package israce

// Enabled reports if the race detector is enabled.
const Enabled = true

      



israce/norace.go

:

// +build !race

// Package israce reports if the Go race detector is enabled.
package israce

// Enabled reports if the race detector is enabled.
const Enabled = false

      

Because of the build tag, only one of the two files will be compiled.

As far as I could find this, this is the easiest way.

+5


source







All Articles