dsiimyoc804955 2017-07-06 09:28 采纳率: 100%
浏览 30
已采纳

如何检查运行时是否启用了竞赛检测器?

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条回答 默认 最新

  • donkey199024 2017-07-06 09:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)