# Golang debugging by using Delve

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052749603/f185848c-8d3b-4295-b056-f246df1b3a8a.gif align="left")

Golang in action

### Diving deep into the GO debugger tool: Delve

Using Delve, 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 details.

Let us first install the Delve debugger tool.

### Downloading and installing Go 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`](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/dlv@v1.7.3`

`$ go install github.com/go-delve/delve/cmd/dlv@v1.7.4–0.20211208103735–2f13672765fe`

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

* · Make sure GOBIN env variable is properly set, which will indicate 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 all the environment variables of your GO environment using the “**go env**” command. You can run the **go env** command on the command prompt, the following will the output.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052751602/29dd0dd6-813f-4f38-8c36-6ca2ed63ac60.png align="left")

* 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 as shown in the below picture and add it to the environment variable.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052753015/764c4762-f086-4323-9804-8986c68dd035.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052754637/9fc8d4df-ec1c-4e07-93c4-e5961fcbe79e.png align="left")

* You’ll be able to run the “dlv” command in the terminal.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052756599/7e554719-1190-4f4a-b993-1cdcacaf3702.png align="left")

* In order to start the debug mode, run the following command from your directory: “dlv debug”
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052758371/7a8c4ac4-3f94-4ae8-96c9-8640adfc74b6.png align="left")

* A test environment will be displayed for debugging. You can check the commands for dlv by typing the **help** command.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052760010/ca2b20ff-8419-4da2-bb82-5e24604826f9.png align="left")

You have finally set up our environment, and now you are ready to debug the code !!

Following is the code that we will be using for debugging:

`package main`

`import “fmt”`

`func main() {`

`var a [5]int`

`fmt.Println(“emp:”, a)`

`a[4] = 100`

`fmt.Println(“set:”, a)`

`fmt.Println(“get:”, a[4])`

`fmt.Println(“len:”, len(a))`

`b := [5]int{1, 2, 3, 4, 5}`

`fmt.Println(“dcl:”, b)`

`var twoD [2][3]int`

`for i := 0; i < 2; i++ {`

`for j := 0; j < 3; j++ {`

`twoD[i][j] = i + j`

`}`

`}`

`fmt.Println(“2d: “, twoD)`

`}`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052761437/25a3590d-131f-427d-b29f-2d78ebdb29ea.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052762827/75a4bf16-a596-483d-96ec-30bbea51af43.png align="left")

* Following are the steps for using Delve debugging tool:
    

1. Initially, we’ll put the breakpoint in the main function using the “break main. main” command.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052764088/fd51a386-654e-49f2-839f-c80be589fbd3.png align="left")

2\. Let’s put a break at lines 8, 12, and 25.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052765632/b1f2093e-36cb-4e56-b672-863c91486861.png align="left")

3\. Type “continue” to see break points in the code in debugging mode.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052767003/2c14444b-57bf-4e93-8985-a866ee1d4769.png align="left")

As shown in the above picture, you can see there is a breakpoint at the main function

4\. As shown in the above image, you can see there is a breakpoint at the main function.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052768670/8e15f2af-5fe7-4762-a675-48a978fb777d.png align="left")

5\. Type “continue” again to see the next breakpoint and type “locals” for viewing local variables declared in the line.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052770136/f153760e-0555-4fa4-a7f0-185c13a13e29.png align="left")

6\. If you visit breakpoint at line 12 after typing “continue”, you’ll be able to see the output of above code line 12.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052771808/c60f7238-e336-43d4-b314-f3c7cd67c4f9.png align="left")

7\. After continuing the last line i.e. line 25, you’ll be able to see the following output which states “**Process 9576 has exited with status 0**” if there is no error in your code.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052773160/4a2434a3-bd12-44ca-bb2f-1f31474f8be0.png align="left")

8\. To exit from the test environment type the **exit** command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052774713/8af56924-4bd8-4766-bc51-5059e91f1bd1.gif align="left")

### You have finally debugged the code using delve …. 👍

**For any further Queries or anything related to Golang Development,Coding, Blogging, Tech Documentation you can DM me on** [**Linkedin**](https://in.linkedin.com/in/anubhav-chaturvedi-a7465a72) **or instagram id=**[**acanubhav94**](http://instagram.com/acanubhav94)**.**

**Special credits to my team members:** [**Shreyas**](https://www.linkedin.com/in/shreyas-patel-5a39a3199/) **and** [**Anshika**](https://www.linkedin.com/in/anshika-yadav-0a69381b7/)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703052776258/17011729-fdab-4bac-a5f8-e7761e2ad8b4.png align="left")

#### If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

#### 🚀Developers: Learn and grow by keeping up with what matters, [JOIN FAUN.](https://faun.to/8zxxd)
