Use Delve to debug GoLang program

Summary

Delve is highlighted as the superior debugger for Go programs, offering better understanding of the Go runtime, data structures, and expressions than GDB. To initiate a debugging session, developers simply run dlv debug from the command line within their project directory. Once in the Delve prompt, key commands like break main.go:line_number enable setting breakpoints, while continue resumes program execution. Developers can inspect variable states using print variable_name and step through code line by line with n, providing a streamlined approach to diagnose and resolve issues in Go applications. This method simplifies the process of understanding program flow and data changes.

If you don't know how to debug program, you are not a real programmer.

gdb can be used to debug go program, but according to golang website, delve is a better option.

 

Note that Delve is a better alternative to GDB when debugging Go programs built with the standard toolchain. It understands the Go runtime, data structures, and expressions better than GDB.

Below is a simple go program.

package main
type Person struct {
	Name string
	Age int
}
func main() {
	var me Person
	me.Name = "Melvin"
	me.Age = 41
	var wife Person
	wife.Name = "Raye"
	wife.Age = 36
	var daughter Person
	daughter.Name = "Katherina"
	daughter.Age = 5
	var son Person
	son.Name = "Vito"
	son.Age = 2
	var family []Person
	family = append(family, me, wife, daughter, son)
	birthdayToday(&daughter)
}
func birthdayToday(person *Person) {
	person.Age = person.Age + 1
}

When using delve to debug, on command console, you just need to run below command.

dlv debug.

And then you can set breakpoint and start the debug journey.

(dlv) help
(dlv) break main.go:29
(dlv) break main.go:31
(dlv) breakpoints
(dlv) continue
(dlv) print family
(dlv) n
(dlv) print family

The output would be like:

Simple enough.

DEBUG GOLANG DELVE

  RELATED

  COMMENTS

0

No comment for this article.