Delve

The delve program is a debugger for Golang and should feel at home to people familiar with gdb.

Install delve

Install the latest release (1.18+) to support Go 1.18beta2 with the following command:

  1. go install github.com/go-delve/delve/cmd/dlv@latest

Configure delve

Even with the latest release, dlv debug may not work as expected”

  1. Consider the following Go program:

    1. package main
    2. import "fmt"
    3. func print[T any](t T) {
    4. fmt.Println(t)
    5. }
    6. func main() {
    7. print("Hello, world.")
    8. }
  2. Save the program as main.go

  3. Run the program through the debugger:

    1. $ dlv debug main.go
    2. # command-line-arguments
    3. ./main.go:5:6: missing function body
    4. ./main.go:5:11: syntax error: unexpected [, expecting (
    5. 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:

  1. 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’s PATH:

    1. export PATH="$(go1.18beta2 env GOROOT)/bin:${PATH}"
  2. Verify that go version shows Go 1.18beta2:

    1. $ go version
    2. go version go1.18beta2 linux/amd64
  3. Run the delve debugger again:

    1. $ dlv debug main.go
    2. Type 'help' for list of commands.
    3. (dlv)

And that’s it! Delve should now be set up to successfully build and debug Go1.18 programs!


Next: Configuring VS Code