调试使用Visual Studio Code和Delve调试器中的标记

问题描述 投票:2回答:2

答:基于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
2个回答
1
投票

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

  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'以禁用编译器优化。

enter image description here


0
投票

Visual Studio Code Go plugin现在支持一个名为launch.jsonbuildFlags密钥,它允许您使用相应的"-tags Tag"值指定构建标记。 (似乎有a bug disallowing multiple tags。)。

相关的excerpt from the plugin Wiki

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

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

© www.soinside.com 2019 - 2024. All rights reserved.