Environment configuration
delve warehouse, including tutorials: https://github.com/go-delve/delve
Golang debugging tutorial: https://github.com/golang/vscode-go/wiki/debugging
> go version
go version go1.20 windows/amd64
> go install github.com/go-delve/delve/cmd/dlv@latest
shortcut key
F8 下一步
F7 进入函数
Shift+F8 退出函数
F9 下一个断点
There are three modes of debugging in golang
1、Launch package
// Launch package: Debug/test the package of the open file
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"args": ["new", " conda"],
"program": "${fileDirname}"
}
It can be seen from the description that this is the package where the currently opened file is debugged, so the currently opened file needs to be the main package, which contains the main function, or a test file, that is, a file that can be run directly. By analogy, we directly enter the directory to executego run
You can bring running parameters args
test program
ctl/
ctl.go
package main
import (
"fmt"
"voteapi/ctl/cmd"
)
func main() {
fmt.Println("xxx")
cmd.Execute()
}
Set fmt.Println("xxx")
a breakpoint, because the debugger will jump directly to the first breakpoint. If there is no breakpoint, it will end directly.
console output
Starting: D:\dev\php\magook\trunk\server\golang\path\bin\dlv.exe dap --listen=127.0.0.1:20155 from D:\dev\php\magook\trunk\server\voteapi\ctl
DAP server listening at: 127.0.0.1:20155
Type 'dlv help' for list of commands.
mode
parameter value
debug
: build and debug a main packagetest
: build and debug a testexec
: debug a precompiled binary- The binary must be built with
go build -gcflags=all="-N -l"
to disable inlining and optimizations that can interfere with debugging.
- The binary must be built with
auto
: automatically choose betweendebug
andtest
depending on the open file
2、Attach to process
// Attach to local process: Attach to an existing process by process ID
{
"name": "Attach to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": 0
}
attach: You can use this configuration to attach to a running process or a running debug session.
mode
parameter value
- local: attaches to a local process.
- The binary must be built with
go build -gcflags=all="-N -l"
to disable inlining and optimizations that can interfere with debugging.
- The binary must be built with
remote
: attaches to an in-progress debug session run by an external server.
You can debug an already running program using the local
mode type configuration. The Go extension will start dlv dap
and configure it to attach to the specified process. Users can select the process to debug with one of the following options:
- Specifying the numeric process id (PID) with the
processId
attribute. - Specifying the target program name in the
processId
attribute. If there are multiple processes matching the specified program name, the extension will show the list of matching processes at the start of the debug session. - Specifying
0
in theprocessId
attribute and selecting the process from the drop-down menu at the start of the debug session.
The application scenario of attach's local mode is a memory-resident program, such as http server. After we compile and run the program, we come to VScode and mainly configure the processId attribute, which can be the process ID, the process name, or 0, and then edit The browser will pop up a drop-down box for you to select the program you want to attach.
Example, still the ctl.go file, change it
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", "hello")
})
http.ListenAndServe(":9999", nil)
}
go build ctl.go
ctl.exe
We choose to Attach to Process
click the run debugging triangle button and enter in the pop-up drop-down box ctl.exe
to start debugging. In the same way, we need to set a few breakpoints first.
The debugging toolbar now looks like this
It is in a suspended state. When we request this service in the browser, it will be activated and we can debug step by step.
After a request is debugged, the debugging will not end, but will be paused, waiting for the next request to be triggered.
3、Connect to server
Instructions https://github.com/golang/vscode-go/wiki/debugging#remote-debugging
This is the Remote debugging function of vscode. It is based on Remote Development, that is, remote development and remote debugging. This also requires the installation of additional vscode extensions to map the remote code to the local. Then the local code can be pushed to the remote server from time to time, and finally in the remote Run the code on the server.
I came here directly and found that I can debug it.
package main
import (
"fmt"
"voteapi/ctl/cmd"
)
func main() {
fmt.Println("xxx")
cmd.Execute()
}
// Connect to server: Connect to a remote headless debug server
{
"name": "Connect to server",
"type": "go",
"debugAdapter": "dlv-dap",
"request": "attach",
"mode": "remote",
"remotePath": "${workspaceFolder}",
"port": 2345,
"host": "127.0.0.1"
}
dlv debug --headless --listen=:2345
launch.json configuration items
Documentation https://github.com/golang/vscode-go/wiki/debugging#configuration
// 命令行参数,要自己打空格
"args": ["-config", " server.json"],
// 编译参数
"buildFlags": "-tags 'server'",
// key:value
"env": {
},