GoLand+Delve 远程调试Go程序 | Go主题月

2,136 阅读1分钟

在Windows或macOS上开发,在Linux部署,是服务端开发中常见的场景。对于go程序,可以直接在Linux上使用gdb或是Delve进行调试,但显然没有Goland这类IDE断点调试来得爽。本文介绍GoLand+Delve进行远程调试go程序的步骤。

安装Delve

Delve是go程序的一个调试工具。它的github地址如下: github.com/derekparker…

根据它的介绍,有两种安装模式:go get模式或是make install模式。

本文,采用go get 模式

go get github.com/go-delve/delve/cmd/dlv

需要注意的

  • 如果使用的go mod模式,则不要在一个go module目录执行上述命令
  • 记得把GOPATH/bin加入环境变量GOPATH/bin加入环境变量PATH中

配置GoLand的Remote Debug

本文以GoLand 2021.3.2为例,介绍配置Remote Debug的步骤。

  1. 点击Run-> Edit Configuration...

image.png

  1. 点击配置界面右上角的+,新增Go Remote配置

测试配置成功

  1. 在Linux环境编译go工程,如
go build -gcflags "all=-N -l" main.go
  1. 用dlv启动
dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./main
  1. 在GoLand中启动调试

image.png