如何检查比赛检测器是否在运行时启用? [英] How can I check if the race detector is enabled at runtime?
问题描述
有什么方法可以检查Go程序是否在运行时启用了 -race >(例如用于日志/信息目的)?
我检查了文档以及明显的位置( runtime / *
),但我找不到任何东西。
据我所知,没有简单的检查,但是当 -race
被启用时, race
构建标签已设置,因此您可以利用该功能。
我创建了一个新目录 israce
,并在其中放置了两个文件:
// +构建比赛
//如果Go比赛检测器已启用,则打包israce报告。
包israce
//启用比赛检测器时启用报告。
const Enabled = true
由于构建标记,两个文件中只有一个会得到编译。 据我所能发现这是最简单的方法。 Is there any way I can check if the Go program is compiled with I checked the documentation, as well as the obvious locations ( As far as I can find there is no simple check for this, but when I made a new directory Due to the build tag only one of the two files will get compiled. As far as I've been able to find this is the easiest way. 这篇关于如何检查比赛检测器是否在运行时启用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! israce / norace.go < code $:b
$ b $ $ p $ // + build!race
//包israce报告if Go比赛检测器已启用。
包israce
//启用比赛检测器时启用报告。
const Enabled = false
-race
enabled at runtime (for e.g. logging/informational purposes)?runtime/*
), but I can't find anything.-race
is enabled the race
build tag is set, so you can take advantage of that.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