Go 如何在运行时检查竞赛检测器是否启用?

Go 如何在运行时检查竞赛检测器是否启用?,go,Go,是否有任何方法可以检查Go程序是否在运行时启用了-race(例如,出于日志记录/信息目的)的情况下编译 我检查了,以及明显的位置(runtime/*),但我什么也找不到。据我所知,没有简单的检查,但是当启用-race时,设置了race,因此您可以利用它 我创建了一个新目录israce,并在其中放置了两个文件: israce/race.go: // +build race // Package israce reports if the Go race detector is enabled.

是否有任何方法可以检查Go程序是否在运行时启用了
-race
(例如,出于日志记录/信息目的)的情况下编译


我检查了,以及明显的位置(
runtime/*
),但我什么也找不到。

据我所知,没有简单的检查,但是当启用
-race
时,设置了
race
,因此您可以利用它

我创建了一个新目录
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
// +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
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 = true
// +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
由于build标记,两个文件中只有一个将被编译


Go标准库也是这样做的(,),但由于这是一个内部包,因此不能在Go源代码库之外导入。

非常感谢,请为您的代码提供许可证,或将其转换为库。如此简单和明显的内容甚至不符合版权@VitalyIsaev;堆栈溢出上的所有内容都已经是CC BY-SA了。如果你一定要有执照,那么我把它放在麻省理工学院。但正如我所说,版权并不真正适用,它已经是CC BY-SA了。如果你(或任何人)只是复制/粘贴它而没有任何归属,我当然没有问题。