英文:
Debugging Go with tags in Visual Studio Code and Delve debugger
问题
根据putus的答案,我找到了以下配置,可以实现一键构建和调试。
首先,你需要添加一个任务来使用相应的标签构建二进制文件。
{
"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", // 添加的配置
"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
}
]
}
英文:
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
}
]
}
答案1
得分: 6
Visual Studio Code Go插件现在支持一个名为buildFlags
的launch.json
键,允许您使用相应值"-tags Tag"
指定构建标签(存在一个不允许多个标签的错误)。
插件Wiki中相关的摘录如下:
如果您的构建需要构建标签(例如
go build -tags whatever_tag
),则添加参数buildFlags
,内容为"-tags whatever_tag"
。
如果您有不同的构建配置,每个配置都需要自己的构建标签,您可以为每个配置创建单独的启动配置。
英文:
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.
答案2
得分: 1
你可以将预构建的二进制文件附加到调试器上。
- 通过命令行构建应用程序,例如
go build -o myapp.exe -tags THISISAFLAG
。 - 在
launch.json
中添加配置Launch Exe
。
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Debug", //现有配置
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
},
{
"name": "Launch EXE", //新增配置
"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'
来禁用编译器优化。
英文:
You can attach pre-built binary to debugger.
-
Build the application from command line, e.g.
go build -o myapp.exe -tags THISISAFLAG
-
Add configuration
Launch Exe
tolaunch.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.
答案3
得分: 0
以下是我的测试配置:
{
"name": "Delve: Test",
"type": "go",
"request": "launch",
"mode": "test",
"buildFlags": "-tags 'unit_tests integration_tests all_tests'",
"program": "${file}",
"showLog": true
}
- 除非所选文件中不存在这些标签,否则它将持续出错。
- 根据您的用例,您可能需要将
"${file}"
更改为"${fileDirname}"
,如果您使用后者,至少一个文件应该具有所提到的标签。
英文:
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
}
- It will keep erroring out unless the tags don't exist in the selected file.
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论