Delve
The delve program is a debugger for Golang and should feel at home to people familiar with gdb
.
- Install delve: install the go debugger
- Configure delve: ensure delve uses Go 1.18beta2
Install delve
Install the latest release (1.18+) to support Go 1.18beta2 with the following command:
go install github.com/go-delve/delve/cmd/dlv@latest
Configure delve
Even with the latest release, dlv debug
may not work as expected”
Consider the following Go program:
package main
import "fmt"
func print[T any](t T) {
fmt.Println(t)
}
func main() {
print("Hello, world.")
}
Save the program as
main.go
Run the program through the debugger:
$ dlv debug main.go
# command-line-arguments
./main.go:5:6: missing function body
./main.go:5:11: syntax error: unexpected [, expecting (
exit status 2
This fails because delve uses the go
binary from the shell’s PATH
to build a binary suitable for debugging. To fix this issue the Go 1.18 binary needs to be the program called when delve invokes the go
command:
If Go 1.18beta2 was installed from the instructions on the previous page, then use the following command to ensure that Go 1.18beta2’s
go
binary appears first in the shell’sPATH
:export PATH="$(go1.18beta2 env GOROOT)/bin:${PATH}"
Verify that
go version
shows Go 1.18beta2:$ go version
go version go1.18beta2 linux/amd64
Run the delve debugger again:
$ dlv debug main.go
Type 'help' for list of commands.
(dlv)
And that’s it! Delve should now be set up to successfully build and debug Go1.18 programs!
Next: Configuring VS Code