有什么方法可以检查Go程序是否在运行时启用了 -race >(例如用于日志/信息目的)?
我检查了文档以及明显的位置( runtime / * ),但我找不到任何东西。
解决方案据我所知,没有简单的检查,但是当 -race 被启用时, race 构建标签已设置,因此您可以利用该功能。
我创建了一个新目录 israce ,并在其中放置了两个文件:
israce / race.go :
// +构建比赛 //如果Go比赛检测器已启用,则打包israce报告。 包israce //启用比赛检测器时启用报告。 const Enabled = trueisrace / norace.go < code $:b $ b $ $ p $ // + build!race //包israce报告if Go比赛检测器已启用。 包israce //启用比赛检测器时启用报告。 const Enabled = false
由于构建标记,两个文件中只有一个会得到编译。
据我所能发现这是最简单的方法。
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 = trueisrace/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 = falseDue 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.
更多推荐
如何检查比赛检测器是否在运行时启用?
发布评论