VSCode远程调试Go程序方法,Attach

  1. set launch.json
        {
            "name": "Attach",
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "remotePath": "${workspaceRoot}",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}",
            "env": {},
            "args": [],
            "showLog": true
        }
  1. go build && run
  2. get pid && dlv attach $pid --headless=true --api-version=2--listen=:2345 --log
  3. in VSCode, Click begin debug button

* 注意go build要带上这个标记 -gcflags='-N -l',目的是关闭Go内部做的一些优化,聚合变量和函数内联等优化。

* 或者直接这样运行dlv --listen=:2345 --headless=true --api-version=2 exec ./demo