Wednesday, February 28, 2018

Debugging Go programs with Delve

What is Delve?
Delve aims to be a very simple and powerful tool, but can be confusing if you are not used to using a source level debugger in a compiled language. Tracking down bugs in your code can be a very frustrating experience. This is even more true of highly parallel code. Having a good debugger at your disposal can make all the difference when it comes to tracking down a difficult, or hand to reproduce bug in your code.


Implementation
I am using go version  go1.8 windows/amd64. (Delve debugger can run in Linux, Windows and macOS.

Let's create a simple code in golang to demonstrate Delve debugger. The code shown as below:

filename: main.go
package main

import "fmt"

func main() {
    fmt.Println("Hello World")
    fmt.Println("Hello World")
    fmt.Println("Hello World")
}


Open your windows command prompt to perform Delve debugger by pressing  Windows Key + R and type cmd then press enter. In command prompt, type command
dlv debug main.go


Then you will enter to dlv debug mode


to check delve command you can type help and it will shown all command that can be used for debugging


Let's add break-point at line 6 by using command
break main.go:6
add break-point at line 7 by using command
break main.go:7
Then you can start debug by using command
continue
and it will stop at break-point line 6

if you want to continue to the next break point, just using command
next
and it will go to the next break-point line 7




You can see the break-point with simbol  => (blue color)   it's in yellow box
command is in red box and output is in green box, shown as picture below



I think that's all. Thank you


Other topics:




1 comment:

  1. Thank you for sharing this! What you have shared is very helpful and informative. Would love to see more updates from you.

    Website Development

    ReplyDelete