在 Visual Studio Code 和 Delve 调试器中使用标签调试 Go

Dav*_*vid 4 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,
  "args": [""],
  "showOutput": "always",
  "tasks": [
        {
            "taskName": "buildBinWithTag",
            "command": "go",
            "args": ["build", "-o", "BinaryName", "-tags", "THISISATAG"],
            "isShellCommand": true            
        }       
    ]
}
Run Code Online (Sandbox Code Playgroud)

此任务应在调试器启动之前执行。

{
  "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"
    }
  ]
} 
Run Code Online (Sandbox Code Playgroud)

原始问题:我使用构建标签来编译 Go 程序的不同版本,并使用“go build -tags THISISAFLAG”进行编译

//+build THISISAFLAG

package main
Run Code Online (Sandbox Code Playgroud)

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

{
  "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
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

jpa*_*pap 8

Visual Studio Code Go 插件现在支持一个launch.json名为 的键buildFlags,该键允许您指定具有相应值 的构建标记"-tags Tag"。(似乎存在不允许多个标签的错误。)。

插件 Wiki 的相关摘录:

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

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