A debugging example

If ~/go/bin is in your PATH environment variable, then you can call Delve from everywhere as dlv. Otherwise, you will need to provide its full path. I am going to use the full path in this subsection.

The first Delve command that you should know is debug. This command will tell Delve to compile the main package in the current working directory and begin to debug it. If there is no main package in the current working directory, you will get the following error message:

$ ~/go/bin/dlv debug
go: cannot find main module; see 'go help modules'
exit status 1
  

So, let us go to the ./ch07/debug directory and debug a real program that is stored in main.go. The Go code of main.go is as follows:

package main import ( "fmt" "os" ) ...

Get Mastering Go - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.