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

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

图片转代码服务由CSDN问答提供 功能建议

答案: 根据问题解答,我确定了以下配置以进行构建和 一键调试

首先,您需要添加一个任务来构建带有相应标签的二进制文件。

  {
 /  /请参阅https://go.microsoft.com/fwlink/?LinkId=733558
 //有关task.json格式的文档
“ version”:“ 0.1.0”,
“ command”:“  bash“,
” isShellCommand“:true,
” args“:[”“],
” showOutput“:”始终“,
” tasks“:[
 {
” taskName“:” buildBinWithTag  “,
” command“:” go“,
” args“:[” build“,” -o“,” BinaryName“,” -tags,“ THISISATAG”],
“ isShellCommand”:true \  n} 
] 
} 
   
 
 

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

  {  
“ version”:“ 0.2.0”,
“ configuration”:[
 {
“ name”:“ DebugBinWithTag”,//添加了config 
“ type”:“ go”,
“ requ  est”:“启动”,
“模式”:“ exec”,
“ remotePath”:“”,
“端口”:2345,
“主机”:“ 127.0.0.1”,
“程序 “:” $ {workspaceRoot} / BinaryName“,
” env“:{},
” args“:[],
” showLog“:true,
” preLaunchTask“:” buildBinWithTag“ 
} \  n] 
} 
   
 
 

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

  // +构建THISISAFLAG 
 
package main 
   
 
 

完美地工作。 但是有没有办法告诉调试器使用这些标志。 我尝试使用类似以下的启动配置,但是没有用。

  {
“ version”:“ 0.2.0”,
“ 配置”:[
 {
“名称”:“启动”,
“类型”:“执行”,
“请求”:“启动”,
“模式”:“调试”,
“  remotePath“:”“,
”端口“:2345,
”主机“:” 127.0.0.1“,
”程序“:” $ {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

    点赞 打赏 评论
  • dongzhan5286 2018-08-30 18:59

    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". (There appears to be a bug disallowing multiple tags.).

    Relevant excerpt from the plugin Wiki:

    If your build needs build tags (e.g. go build -tags whatever_tag), then add the parameter buildFlags with the content "-tags whatever_tag".

    If you have different build configurations each requiring their own build tag, you can create separate launch configurations for each.

    点赞 打赏 评论

相关推荐