简体   繁体   English

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

[英]Debugging Go with tags in Visual Studio Code and Delve debugger

Answer: Based on putus answer, I figured out the following configuration to build and debug with one click答:根据putus的回答,我想出了以下配置,一键构建调试

At first you need to add a task to build the binary with the respective tags.首先,您需要添加一个任务来构建带有相应标签的二进制文件。

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

This task should be executed before the debugger launches.此任务应在调试器启动之前执行。

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

Original question :I'm using build tags for compiling different versions of a Go program and I compile it with "go build -tags THISISAFLAG"原始问题:我正在使用构建标签来编译 Go 程序的不同版本,并使用“go build -tags THISISAFLAG”进行编译

//+build THISISAFLAG

package main

This works perfectly.这完美地工作。 But is there a way to tell the debugger to use these flags.但是有没有办法告诉调试器使用这些标志。 I've tried to use a launch configuration like the following, but it didn't work.我尝试使用如下所示的启动配置,但没有奏效。

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

The Visual Studio Code Go plugin now supports a launch.json key called buildFlags that allows you to specify the build tags with a corresponding value of "-tags Tag" . Visual Studio Code Go 插件现在支持名为buildFlagslaunch.json键,它允许您使用相应的"-tags Tag"值指定构建标记。 (There appears to be a bug disallowing multiple tags .). (似乎存在一个不允许使用多个标签的错误。)。

Relevant excerpt from the plugin Wiki : 插件 Wiki 的相关摘录

If your build needs build tags (eg go build -tags whatever_tag), then add the parameter buildFlags with the content "-tags whatever_tag".如果您的构建需要构建标签(例如 go build -tagswhatever_tag),则添加带有内容“-tagswhatever_tag”的参数 buildFlags。

If you have different build configurations each requiring their own build tag, you can create separate launch configurations for each.如果您有不同的构建配置,每个配置都需要自己的构建标记,您可以为每个配置创建单独的启动配置。

You can attach pre-built binary to debugger.您可以将预构建的二进制文件附加到调试器。

  1. Build the application from command line, eg go build -o myapp.exe -tags THISISAFLAG从命令行构建应用程序,例如go build -o myapp.exe -tags THISISAFLAG
  2. Add configuration Launch Exe to launch.json将配置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 } ] }

Note:笔记:

Due to compiler optimization and this issue , some variables may not being displayed or displayed with different name during debug session (see below).由于编译器优化和此问题,在调试会话期间,某些变量可能不会显示或以不同的名称显示(见下文)。 In the future, you may add -gcflags='-N -l' when building the application to disable compiler optimization.将来,您可以在构建应用程序时添加-gcflags='-N -l'以禁用编译器优化。

在此处输入图片说明

Here are my test configurations:这是我的测试配置:

{
  "name": "Delve: Test",
  "type": "go",
  "request": "launch",
  "mode": "test",
  "buildFlags": "-tags 'unit_tests integration_tests all_tests'",
  "program": "${file}",
  "showLog": true
}
  1. It will keep erroring out unless the tags don't exist in the selected file.除非所选文件中不存在标签,否则它将不断出错。
  2. You may want to change "${file}" to "${fileDirname}" depending on your usecase, if you are using the latter, at least one of the files should have the mentioned tag.您可能希望根据您的用例将"${file}"更改为"${fileDirname}" ,如果您使用后者,则至少其中一个文件应具有上述标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM