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

编程入门 行业动态 更新时间:2024-10-26 06:29:05
本文介绍了如何检查比赛检测器是否在运行时启用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有什么方法可以检查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.

更多推荐

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

本文发布于:2023-11-06 04:00:47,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!