安装go环境
wget https://dl.google.com/go/go1.18.4.linux-amd64.tar.gz
tar -zxvf go1.18.4.linux-amd64.tar.gz -C /usr/local/lib/
ln -s /usr/local/lib/go/bin/* /usr/bin/
# 可选配置
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go version
安装dlv
go install github.com/go-delve/delve/cmd/dlv@latest
ln -s /root/go/bin/dlv /usr/local/bin/dlv
查看dlv版本
[root@lys-mysql go]# dlv version
Delve Debugger
Version: 1.9.1
Build: $Id: d81b9fd12bfa603f3cf7a4bc842398bd61c42940 $
添加debug方案
修改为远程调试
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Connect to server",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "/root/go/src/hello",
"port": 2345,
"host": "10.6.8.248"
}
]
}
启动服务层
[root@lys-mysql-ha0826001 hello]# dlv debug hello.go --headless --listen ":2345" --log --api-version 2
API server listening at: [::]:2345
2022-08-29T15:09:09+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2022-08-29T15:09:09+08:00 info layer=debugger launching process with args: [/root/go/src/hello/__debug_bin]
2022-08-29T15:09:17+08:00 info layer=debugger created breakpoint: &api.Breakpoint{ID:1, Name:"", Addr:0x498106, Addrs:[]uint64{0x498106}, File:"/root/go/src/hello/hello.go", Line:10, FunctionName:"main.main", Cond:"", HitCond:"", HitCondPerG:false, Tracepoint:false, TraceReturn:false, Goroutine:false, Stacktrace:0, Variables:[]string(nil), LoadArgs:(*api.LoadConfig)(0xc0003ee330), LoadLocals:(*api.LoadConfig)(0xc0003ee390), WatchExpr:"", WatchType:0x0, VerboseDescr:[]string(nil), HitCount:map[string]uint64{}, TotalHitCount:0x0, Disabled:false, UserData:interface {}(nil)}
2022-08-29T15:09:17+08:00 info layer=debugger created breakpoint: &api.Breakpoint{ID:2, Name:"", Addr:0x498125, Addrs:[]uint64{0x498125}, File:"/root/go/src/hello/hello.go", Line:13, FunctionName:"main.main", Cond:"", HitCond:"", HitCondPerG:false, Tracepoint:false, TraceReturn:false, Goroutine:false, Stacktrace:0, Variables:[]string(nil), LoadArgs:(*api.LoadConfig)(0xc0005267e0), LoadLocals:(*api.LoadConfig)(0xc000526810), WatchExpr:"", WatchType:0x0, VerboseDescr:[]string(nil), HitCount:map[string]uint64{}, TotalHitCount:0x0, Disabled:false, UserData:interface {}(nil)}
2022-08-29T15:09:17+08:00 info layer=debugger created breakpoint: &api.Breakpoint{ID:3, Name:"", Addr:0x498132, Addrs:[]uint64{0x498132}, File:"/root/go/src/hello/hello.go", Line:14, FunctionName:"main.main", Cond:"", HitCond:"", HitCondPerG:false, Tracepoint:false, TraceReturn:false, Goroutine:false, Stacktrace:0, Variables:[]string(nil), LoadArgs:(*api.LoadConfig)(0xc0003ee630), LoadLocals:(*api.LoadConfig)(0xc0003ee690), WatchExpr:"", WatchType:0x0, VerboseDescr:[]string(nil), HitCount:map[string]uint64{}, TotalHitCount:0x0, Disabled:false, UserData:interface {}(nil)}
2022-08-29T15:09:17+08:00 debug layer=debugger continuing
2022-08-29T15:09:25+08:00 debug layer=debugger continuing
2022-08-29T15:09:27+08:00 debug layer=debugger continuing
2022-08-29T15:09:29+08:00 debug layer=debugger continuing
3 + 2 = 5
2022-08-29T15:09:29+08:00 debug layer=debugger continuing
2022-08-29T15:09:29+08:00 debug layer=debugger detaching