首页  >  文章  >  后端开发  >  Visual Studio Code 的调试器可以使用 Go 程序的构建标签吗?

Visual Studio Code 的调试器可以使用 Go 程序的构建标签吗?

Patricia Arquette
Patricia Arquette原创
2024-10-24 13:18:02199浏览

Can Visual Studio Code's Debugger Use Build Tags for Go Programs?

在 Visual Studio Code 和 Delve 中进行调试标记

使用构建标记调试 Go 程序可能是一个挑战,尤其是在使用 Visual Studio Code 和 Delve 调试器时。

原问题:

调试器可以配置为在编译 Go 程序时使用指定的构建标签吗?

答案:

Visual Studio Code 的 Go 插件现在支持 launch.json 中的 buildFlags 键。这允许开发人员使用“-tags Tag”格式指定构建标签。

配置:

使用标签构建二进制文件的任务:

<code class="json">{
  "version": "0.1.0",
  "command": "bash",
  "isShellCommand": true,
  "args": [""],
  "showOutput": "always",
  "tasks": [
    {
      "taskName": "buildBinWithTag",
      "command": "go",
      "args": ["build", "-o", "BinaryName", "-tags", "THISISATAG"],
      "isShellCommand": true
    }
  ]
}</code>

启动调试配置:

<code class="json">{
  "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"
    }
  ]
}</code>

示例:

使用构建标签“build THISISAFLAG” :

<code class="go">//+build THISISAFLAG

package main</code>

启动配置:

<code class="json">{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "DebugWithTag",
      "type": "go",
      "request": "launch",
      "mode": "exec",
      ...
      "buildFlags": "-tags THISISAFLAG",
      ...
    }
  ]
}</code>

此配置将确保调试器在编译期间使用指定的构建标记,从而允许执行和调试具有不同标签相关配置的 Go 程序。

以上是Visual Studio Code 的调试器可以使用 Go 程序的构建标签吗?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn