如何检查比赛检测器是否在运行时启用? [英] How can I check if the race detector is enabled at runtime?

查看:125
本文介绍了如何检查比赛检测器是否在运行时启用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检查Go程序是否在运行时启用了 -race >(例如用于日志/信息目的)?



我检查了文档以及明显的位置( runtime / * ),但我找不到任何东西。

解决方案

据我所知,没有简单的检查,但是当 -race 被启用时, race 构建标签已设置,因此您可以利用该功能。



我创建了一个新目录 israce ,并在其中放置了两个文件:

israce / race.go

  // +构建比赛

//如果Go比赛检测器已启用,则打包israce报告。
包israce

//启用比赛检测器时启用报告。
const Enabled = true

israce / 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 = 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.

As far as I've been able to find this is the easiest way.

这篇关于如何检查比赛检测器是否在运行时启用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆