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

问题描述:

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

Answer: Based on putus answer, I figured out the following configuration to build and debug with one click

首先,您需要添加一个任务以使用相应的标签构建二进制文件.

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

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

Original question:I'm using build tags for compiling different versions of a Go program and I compile it with "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
    }
  ]
}

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

You can attach pre-built binary to debugger.

  1. 从命令行构建应用程序,例如go build -o myapp.exe -tags THISISAFLAG
  2. 将配置Launch Exe添加到launch.json

  1. Build the application from command line, e.g. go build -o myapp.exe -tags THISISAFLAG
  2. Add configuration Launch Exe to 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'以禁用编译器优化.

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.