Debugging 在VisualStudio代码和Delve调试器中使用标记进行调试

Debugging 在VisualStudio代码和Delve调试器中使用标记进行调试,debugging,go,visual-studio-code,Debugging,Go,Visual Studio Code,回答: 基于putus的回答,我想出了以下配置,只需单击一下即可构建和调试 首先,您需要添加一个任务来构建带有相应标记的二进制文件 { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "0.1.0", "command": "bash", "isShellCommand": true,

回答: 基于putus的回答,我想出了以下配置,只需单击一下即可构建和调试

首先,您需要添加一个任务来构建带有相应标记的二进制文件

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "0.1.0",
  "command": "bash",
  "isShellCommand": true,
  "args": [""],
  "showOutput": "always",
  "tasks": [
        {
            "taskName": "buildBinWithTag",
            "command": "go",
            "args": ["build", "-o", "BinaryName", "-tags", "THISISATAG"],
            "isShellCommand": true            
        }       
    ]
}
此任务应在调试器启动之前执行

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "DebugBinWithTag",    //added config
      "type": "go",
      "request": "launch",
      "mode": "exec",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${workspaceRoot}/BinaryName",
      "env": {},
      "args": [],
      "showLog": true,
      "preLaunchTask": "buildBinWithTag"
    }
  ]
} 
原始问题:我正在使用构建标记编译Go程序的不同版本,并使用“Go构建-标记THISISAFLAG”进行编译

这很好用。但是有没有办法告诉调试器使用这些标志呢。我尝试过使用如下的启动配置,但没有成功

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${fileDirname}",
      "env": {},
      "args": ["-flags THISISAFLAG"],
      "showLog": true
    }
  ]
}

您可以将预构建的二进制文件附加到调试器

  • 从命令行生成应用程序,例如
    go Build-o myapp.exe-tags THISISAFLAG
  • 将配置
    Launch Exe
    添加到
    Launch.json

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Launch Debug",  //existing config
          "type": "go",
          "request": "launch",
          "mode": "debug",
          "remotePath": "",
          "port": 2345,
          "host": "127.0.0.1",
          "program": "${fileDirname}",
          "env": {},
          "args": [],
          "showLog": true
        },
        {
          "name": "Launch EXE",    //added config
          "type": "go",
          "request": "launch",
          "mode": "exec",
          "remotePath": "",
          "port": 2345,
          "host": "127.0.0.1",
          "program": "${workspaceRoot}/myapp.exe",
          "env": {},
          "args": [],
          "showLog": true
        }
      ]
    } 
    
  • 注意:

    由于编译器优化和错误,在调试会话期间,某些变量可能不会显示或以不同的名称显示(请参见下文)。将来,您可以在构建应用程序时添加
    -gcflags='-N-l'
    ,以禁用编译器优化

    现在支持名为
    buildFlags
    launch.json
    键,该键允许您使用相应的值
    “-tags Tag”
    指定构建标记。(似乎有。)

    相关:

    如果您的构建需要构建标记(例如go build-tags which_tag),则添加参数buildFlags,内容为“-tags which_tag”

    如果您有不同的构建配置,每个构建配置都需要自己的构建标记,那么您可以为每个构建配置创建单独的启动配置

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Launch Debug",  //existing config
          "type": "go",
          "request": "launch",
          "mode": "debug",
          "remotePath": "",
          "port": 2345,
          "host": "127.0.0.1",
          "program": "${fileDirname}",
          "env": {},
          "args": [],
          "showLog": true
        },
        {
          "name": "Launch EXE",    //added config
          "type": "go",
          "request": "launch",
          "mode": "exec",
          "remotePath": "",
          "port": 2345,
          "host": "127.0.0.1",
          "program": "${workspaceRoot}/myapp.exe",
          "env": {},
          "args": [],
          "showLog": true
        }
      ]
    }