Is there any way I can check if the Go program is compiled with -race
enabled at runtime (for e.g. logging/informational purposes)?
I checked the documentation, as well as the obvious locations (runtime/*
), but I can't find anything.
As far as I can find there is no simple check for this, but when -race
is enabled the race
build tag is set, so you can take advantage of that.
I made a new directory israce
, and put 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
Due to the build tag only one of the two files will get compiled.
This is also how the Go standard library does it (race.go
, norace.go
), but since that's an internal package it can't be imported outside of the Go source base.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With