doushui20090526 2017-04-24 07:20
浏览 160
已采纳

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

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

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
    }
  ]
}
  • 写回答

2条回答 默认 最新

  • doupu1957 2017-04-25 00:11
    关注

    You can attach pre-built binary to debugger.

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

    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.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀