使用 Go 和 GoLand 远程调试 HTTP 请求 IDE

Remote Debugging HTTP requests using Go and GoLand IDE

我是新来的,delve 和 GoLand IDE。我想远程调试一些在 docker 中使用 make 和 运行 部署的 REST 端点(使用 docker + docker-compose)。

我用来调出环境的命令是make myproject

我的端点托管在:localhost:8080

在 GoLand 中创建调试配置时指定在 运行ning 之前 dlv debug --headless --listen=:2345 --api-version 2,执行以下操作:

go build -gcflags='-N -l' github.com/myproject
dlv --listen=:2345 --headless=true --api version=2 exec ./myproject

我的项目在 localhost:8080 上 运行ning 后,有没有办法让我附加到我的项目?如果是这种情况,这些命令将有何不同?

提前致谢

此命令 dlv debug --headless --listen=:2345 --api-version 2 your/package/name 允许 delve 编译包然后启动自身和编译的二进制文件。

另一方面,这些命令 go build -gcflags='-N -l' github.com/myproject dlv --listen=:2345 --headless=true --api version=2 exec ./myproject 向您展示您可以单独编译二进制文件,以防您需要更大的灵活性,然后启动调试器。请记住,添加 -gcflags='-N -l'-gcflags='all=-N -l' 非常重要,这取决于您拥有的 Go 版本,以便调试器能够获得有关您的应用程序的更多可用数据。

至于:

Is there a way for me to attach to my project once it's running on localhost:8080? How will these commands differ if that is the case?

这取决于你在哪里 运行 这个过程。如果它直接在你的机器上,没有 VM 或容器,那么 IDE 有 Run|Attach to Process 中的选项。如果进程在另一台机器上 运行 那么你需要登录到那台机器并使用 dlv --headless --listen=:2345 --api-version 2 attach <pid>.

几个月前我们发布了an article专门针对这个问题,请查看它以获得更详细的回复。