英文:
How can I check if the race detector is enabled at runtime?
问题
有没有办法在运行时检查 Go 程序是否使用 -race
编译(例如用于日志/信息目的)?
我查看了文档,以及显而易见的位置(runtime/*
),但我找不到任何相关信息。
英文:
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.
答案1
得分: 15
自Go 1.18版本开始,有一个名为debug.ReadBuildInfo()
的函数可以提供这个信息,例如:
func race() {
b, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("could not read build info")
return
}
for _, s := range b.Settings {
if s.Key == "-race" {
fmt.Println("-race=" + s.Value)
return
}
}
fmt.Println("-race=false")
}
这段代码会打印出-race=true
或-race=false
。
在大多数情况下,直接使用debug.BuildInfo.String()
方法可能是最简单的方法,因为它还会打印出其他有用的信息。
由于这个函数是从Go 1.18版本开始提供的,在撰写本文时相对较新,你可能需要考虑添加一个// +build go.18
的构建标签,并为旧版本的Go提供一个兼容的代码。
旧版本的Go 1.18之前的解决方案,仅供参考。
据我所知,没有简单的方法来检查这个,但是当启用-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
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
由于构建标签的存在,只有其中一个文件会被编译。
这也是Go标准库的做法(race.go
,norace.go
),但由于它是一个内部包,无法在Go源代码之外导入。
英文:
Since Go 1.18 there's debug.ReadBuildInfo()
, which provides this; for example:
func race() {
b, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("could not read build info")
return
}
for _, s := range b.Settings {
if s.Key == "-race" {
fmt.Println("-race=" + s.Value)
return
}
}
fmt.Println("-race=false")
}
Which will print -race=true
or -race=false
.
In most cases, simply using the debug.BuildInfo.String()
method is probably the easiest, as it prints out other useful stuff as well.
Since this is available since Go 1.18, which is comparatively new at the time of writing, you may want to consider putting a // +build go.18
build tag and provide a shim for older Go versions.
Older pre-Go 1.18 answer, kept for posterity.
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.
This is also how the Go standard library does it (race.go
, norace.go
), but since that's an internal package it can't be imported outside of the Go source base.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论