How to do debugging Golang by using Delve

Golang Debugging

Golang is a compiled programming language designed by Rob Pike, Robert Griesemer, and Ken Thompson at Google.

It is a statically typed language that is syntactically similar to C language but offers additional features like garbage collection, structural typing, memory safety, and CSP-style concurrency.

Getting into the Golang debugger tool: Delve

Using the Delve Debugger, you can view, add and change breakpoints in a Go program, navigate the program either line-wise or through breakpoints, inspect variables, functions, and expression values, and finally analyze all the programs in detail.

Let us first install the Delve debugger tool.

Downloading and installing Golang Delve

Go Delve can be downloaded and installed by using the go get command.

Linux, Windows, OSX

Clone the git repository and build:

$ git clone https://github.com/go-delve/delve$ cd delve$ go install github.com/go-delve/delve/cmd/dlv

Alternatively, on Go version 1.16 or later:

# Install the latest release:$ go install github.com/go-delve/delve/cmd/dlv@latest# Install at tree head:$ go install github.com/go-delve/delve/cmd/dlv@master# Install at a specific version or pseudo-version:$ go install github.com/go-delve/delve/cmd/[email protected]$ go install github.com/go-delve/delve/cmd/[email protected]–0.20211208103735–2f13672765fe

The following elements should already be set up if you have a working Golang installation:

  • · Ensure GOBIN env variable is properly set, indicating the directory where the dlv (Delve) command will be stored. You can check by typing :

-> go env GOBIN

  • Make sure PATH contains GOBIN, which will allow you to run Go binary executables without specifying the absolute path

You can view your Golang environment’s environment variables using the “go env” command. You can run the go env command on the command prompt, the following will the output.

  • The next step is to set the GOBIN path, you can use the following command:
$ go env -w GOBIN=$GOPATH/src/github.com/go-delve/delve/delve/cmd/dlv
  • Copy the path shown in the picture below and add it to the environment variable.