【问题标题】:Debugging Go with tags in Visual Studio Code and Delve debugger在 Visual Studio Code 和 Delve 调试器中使用标签调试 Go
【发布时间】:2017-09-20 18:38:50
【问题描述】:

答案: 根据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 build -tags THISISAFLAG”进行编译

//+build THISISAFLAG

package main

这非常有效。但是有没有办法告诉调试器使用这些标志。我尝试使用如下启动配置,但没有成功。

{
  "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
    }
  ]
}

【问题讨论】:

    标签: debugging go visual-studio-code


    【解决方案1】:

    这是我的测试配置:

    {
      "name": "Delve: Test",
      "type": "go",
      "request": "launch",
      "mode": "test",
      "buildFlags": "-tags 'unit_tests integration_tests all_tests'",
      "program": "${file}",
      "showLog": true
    }
    
    1. 除非标记在所选文件中不存在,否则它将一直出错。
    2. 您可能需要根据您的用例将"${file}" 更改为"${fileDirname}",如果您使用后者,则至少一个文件应具有上述标记。

    【讨论】:

      【解决方案2】:

      Visual Studio Code Go plugin 现在支持名为 buildFlagslaunch.json 键,允许您使用对应值 "-tags Tag" 指定构建标签。 (似乎有a bug disallowing multiple tags。)。

      相关excerpt from the plugin Wiki

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

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

      【讨论】:

        【解决方案3】:

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

        1. 从命令行构建应用程序,例如go build -o myapp.exe -tags THISISAFLAG
        2. 将配置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
              }
            ]
          } 
          

        注意:

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

        【讨论】:

          猜你喜欢
          • 2019-03-09
          • 2016-12-27
          • 1970-01-01
          • 2021-12-22
          • 2016-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多