有两种方法,不管是哪种方法,都需要远程服务器上的源码或编译出的程序和本地保持一致
1、Headless 方式
在服务端执行:
dlv debug ./cmd/app/main.go --headless --listen=:2345 --api-version=2 --accept-multiclient --continue
或
go build -gcflags "all=-N -l" -o app ./cmd/app
dlv exec ./app --headless --listen=:2345 --api-version=2 --accept-multiclient --continue
在 .vscode/launch.json
中配置:
{
"name": "Connect to server",
"type": "go",
"debugAdapter": "legacy",
"request": "attach",
"mode": "remote",
"port": 2345,
"host": "192.168.1.233"
}
2、DAP 方式
在服务端执行:
go build -gcflags "all=-N -l" -o app ./cmd/app
dlv dap --listen=:12345
在客户端配置:
{
"name": "Connect and launch",
"type": "go",
"debugAdapter": "legacy",
"request": "launch",
"port": 12345,
"host": "127.0.0.1", // can skip for localhost
"mode": "exec",
"program": "/absolute/path/to/remote/workspace/program/app",
"substitutePath": [
{ "from": "${workspaceFolder}", "to": "/path/to/remote/workspace" },
]
}
或:
{
"name": "Connect and launch",
"type": "go",
"debugAdapter": "legacy",
"request": "launch",
"port": 2345,
"host": "127.0.0.1", // can skip for localhost
"mode": "debug",
"program": "/absolute/path/to/remote/workspace/cmd/app",
"substitutePath": [
{ "from": "${workspaceFolder}", "to": "/path/to/remote/workspace" },
]
}
更多:https://github.com/golang/vscode-go/blob/master/docs/debugging.md