首页 > 后端开发 > Golang > 正文

设置 Delve 和 Air 以使用 VS Code 调试 Golang

王林
发布: 2024-08-10 06:49:32
原创
694 人浏览过

Setting up Delve and Air to debug Golang with VS Code

VS 代码扩展

在 VS Code Extensions 中搜索 Go 并安装。

  • https://marketplace.visualstudio.com/items?itemName=golang.Go

调试器

您需要安装 Delve 才能在 VS Code 中进行调试(断点、单步执行等)。

go install github.com/go-delve/delve/cmd/dlv@latest
登录后复制

如果你使用 asdf 安装 Go,delve 将位于:

~/.asdf/shims/dlv
登录后复制

您可能想在安装 go 软件包后运行 asdf reshim。

实时重新加载

由于 GoLang 是一种编译语言,因此代码将被编译为单个可执行文件。在开发过程中,进行更改将需要我们不断地重新编译,这可能是一个手动过程,尤其是在 VS Code 中。

我们将使用 https://github.com/air-verse/air 为我们进行实时重新加载。

它是一个命令行工具,只需在项目文件夹中运行一次即可监视更改。

安装

安装软件包。假设你有 go v1.22 或更高版本。

go install github.com/air-verse/air@latest
登录后复制

如果您使用 asdf 安装 Go,air 将位于:

~/.asdf/shims/air
登录后复制

在项目根目录中初始化一个air.toml配置文件

cd ~/myproject
air init
登录后复制

将air.toml [[go build]]命令编辑为:
- all:标志应该应用于构建包中的所有包
- -N:禁用优化以确保生成的代码更接近源代码,以便于调试
- -l:禁用内联优化,其中小函数会就地扩展以减少函数调用的开销,从而更容易调试
- 来自 Delve Reference 的推理

- cmd = "go build -o ./tmp/main ."
+ cmd = 'CGO_ENABLED=0 go build -gcflags=all="-N -l"-o ./tmp/main .'"'
登录后复制

[!信息]
如果满足以下条件,air 将以默认配置运行:

  • air.toml 文件无效
  • 通过运行命令air在项目文件夹中运行它

它不会使用您的air.toml 文件。

编辑 air.toml full_bin 以使用 [[Delve]] 运行构建的二进制文件。

- full_bin = ""
+ full_bin = "dlv exec ./tmp/main --listen=127.0.0.1:2345 --headless=true --api-version=2 --accept-multiclient --continue --log --"
登录后复制

这将在端口 2345 上运行 Delve。

在项目文件夹中运行air。您应该看到以下输出。

> cd ~/my-project
> air
  __    _   ___  
 / /\  | | | |_) 
/_/--\ |_| |_| \_ v1.52.3, built with Go go1.22.5

mkdir ~/my-project/tmp
watching .
!exclude tmp
building...
running...
API server listening at: 127.0.0.1:2345
2024-07-28T18:47:07+07:00 info layer=debugger launching process with args: [./tmp/main]
2024-07-28T18:47:09+07:00 debug layer=debugger entryPoint 0x1006e8000 machoOff 0x100000000
2024-07-28T18:47:09+07:00 warning layer=debugger debug_frame workaround not applied: function internal/abi.(*RegArgs).IntRegArgAddr (at 0x1006e9070) covered by 0x1006e9070-0x1006e9110
2024-07-28T18:47:09+07:00 debug layer=debugger Adding target 11503 "/Users/alaay/projects/scheduleasy/tmp/main"
2024-07-28T18:47:09+07:00 debug layer=debugger continuing
2024-07-28T18:47:09+07:00 debug layer=debugger ContinueOnce
2024/07/28 18:47:09 Starting server on :5602
登录后复制

将 VS Code 附加到 Delve

在 .vscode/launch.config 文件中,添加以下内容:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to Air",
      "type": "go",
      "request": "attach",
      "mode": "remote",
      "port": 2345,
      "host": "127.0.0.1"
    }
  ]
}
登录后复制

在 VS Code 运行和调试 (CMD + SHIFT + D) 中,使用 Attach to Air 开始调试

[!info] VS Code 无法连接
如果 VS Code 无法连接,则很可能 Delve 未在端口 2345 上运行。尝试使用 lsof -i :2345 检查 dlv 是否正在使用该端口运行。如果它正在运行,您应该看到:

$ lsof -i :2345
COMMAND   PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
dlv     15464 username    3u  IPv4 0x201bff14586139e3      0t0  TCP localhost:dbm (LISTEN)
登录后复制

陷阱

Go 是一种编译语言。这意味着代码被编译成二进制文件然后执行。每当我们在 vscode 中更改代码时:

  • 空气会留意变化
  • 重建二进制文件
  • 在 2345 开始 Delve

这意味着 vscode 将断开连接,您需要重新连接 vscode 才能进行 delve。

以上是设置 Delve 和 Air 以使用 VS Code 调试 Golang的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!