duanhe0817825 2017-12-19 02:07 采纳率: 100%
浏览 127
已采纳

如何查看GoLand调试器是否在程序中运行?

In C# the executing program can detect if it's running in the debugger using:

System.Diagnostics.Debugger.IsAttached

Is there an equivalent in Go? I have some timeouts which I would like to be disabled while I am stepping through the code. Thanks!

I am using the GoLand debugger.

  • 写回答

1条回答 默认 最新

  • dongpan1308 2017-12-19 15:32
    关注

    As far as I know, there is no built-in way to do this in the manner you described. But you can do more or less the same using build tags to indicate that the delve debugger is running. You can pass build tags to dlv with the --build-flags argument. This is basically the same technique as I described in How can I check if the race detector is enabled at runtime?

    isdelve/delve.go

    // +build delve
    
    package isdelve
    
    const Enabled = true
    

    isdelve/nodelve.go:

    // +build !delve
    
    package isdelve
    
    const Enabled = false
    

    a.go:

    package main
    
    import (
        "isdelve"
        "fmt"
    )
    
    func main() {
        fmt.Println("delve", isdelve.Enabled)
    }
    

    Running go run a.go will report delve false, and running
    dlv debug --build-flags='-tags=delve' a.go will report delve true.

    You'll have to pass that flag somewhere in the GoLand user interface. I'm not familiar with GoLand, so I can't really help you with that part specifically.


    Alternatively, you can use delve's set command to manually set a variable after starting the debugger.

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

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制