We can use Delve for Go debugging.
- Follow the General install instructions.
Make sure
PATH
containers$GOPATH/bin
which will allow you to run Go binary executables without specifying the absolute path. - If you are using macOS, you can install via HomeBrew with the following command :
brew install delve
- Check your installation
dlv version
You can start a delve debug server for running and debugging specific package or envtest.
make run-delve GO_PACKAGE=./cmd/manager/main.go
make run-delve GO_PACKAGE=./cmd/cli/main.go ARGUMENTS='cluster create test-cluster --termination-policy=Halt'
make test-delve TEST_PACKAGES=./controllers/apps/...
Unlike
go test
supports multiple packages,Delve
needs a single executable to work, it only supports single package.
You can change debug server port for make run-delve
and make test-delve
.
make run-delve DEBUG_PORT=2347
After bug server started, it will show the listen address:
API server listening at: [::]:2347
Delve
support lots of code editors (editor plugins for Delve), you can choose your favorite editor to connect with the listen address for debugging.
This section introduces how to start debugging with the Delve CLI, GoLand and VSCode. Please refer to the Delve or editors documentation for more details.
$ dlv connect 127.0.0.1:2345
Type 'help' for list of commands.
(dlv) break cluster_controller.go:303
(dlv) continue
(dlv) next
(dlv) ......
Click Run — Edit configurations, add new debug configuration, select "Go Remote":
Add debug server listen address to Host
and Port
And then set breakpoints, and run debug.
Create/modify .vscode/launch.json file for connecting debug server:
{
"version": "0.2.0",
"configurations": [
{
"name": "debug envtest",
"type": "go",
"request": "attach",
"mode": "remote",
"remotePath": "",
"port": 2346,
"host": "127.0.0.1",
"showLog": true,
"trace": "log",
"logOutput": "rpc"
}
]
}