3
有什么方法可以检查Go程序是否在运行时启用-race
编译(例如,用于日志记录/信息目的)?如何检查比赛检测器是否在运行时启用?
我检查了documentation,以及明显的位置(runtime/*
),但我找不到任何东西。
有什么方法可以检查Go程序是否在运行时启用-race
编译(例如,用于日志记录/信息目的)?如何检查比赛检测器是否在运行时启用?
我检查了documentation,以及明显的位置(runtime/*
),但我找不到任何东西。
据我所知,没有简单的检查,但当-race
被启用时,race
build tag被设置,所以你可以利用这一点。
我做了一个新的目录israce
,并把两个文件有:
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
由于构建标签仅在两个文件中的一个将被编译。
据我已经能够找到这是最简单的方法。