How to Debug programs using Delve

Build your project so that it creates a binary file

go build

We can start the debugger with the following command

$ dlv debug
Type 'help' for list of commands.

Setting breakpoint to files

Consider the following main.go file

package main

import "fmt"

func main() {
	fmt.Println("vim-go")
	hello := "hello sir how are you"
	fmt.Println(hello)
	hello2 := "ramdom text"
	fmt.Println(hello2)
}

We run dlv debug to initialize the debug session, then we can user the break command to set a breakpoint to a line number of a file

$ (dlv) break main.go:7
Breakpoint 2 set at 0xdf2cd5 for main.main() C:/Users/elias/code/go/delve-example-1/main.go:7

You can see all the breakpoints with breakpoints command

(dlv) breakpoints
Breakpoint 1 (suspended) at main.main
Breakpoint 2 (suspended) at main.go:7

You can delete breakpoints using the clear command, or also use clearall

clear 1