Introduction to dlv
Delve, often referred to as dlv
, is a powerful debugger tailored specifically for the Go programming language. It is widely used by developers to debug Go programs efficiently. This ultimate guide will introduce you to the various API functionalities provided by dlv
and demonstrate their usage with practical examples.
Basic Usage
To get started with dlv
, you need to install it first:
go install github.com/go-delve/delve/cmd/dlv@latest
Once installed, you can start debugging your application with the following command:
dlv debug main.go
Commonly Used APIs
Launching and Attaching to a Program
Launch a program:
dlv debug
dlv exec ./myapp
Attach to a running process:
dlv attach
Breakpoints Management
Set a breakpoint at a specific function or line:
b main.main
b main.go:10
List all breakpoints:
break list
Remove a breakpoint:
clear
Control Execution
Continue program execution:
continue
Step through the code:
step
step-instruction
next
Inspect Program State
Print the value of a variable:
print
Evaluate an expression:
expr
List stack frames:
stack
Configuration and Scripting
Load a configuration file:
dlv --config dlv.toml
Run commands from a script file:
dlv debug --init script.dlv
Example Application
Consider a simple Go application for illustration:
package main
import "fmt"
func main() {
fmt.Println("Hello, Delve!")
result := add(3, 4)
fmt.Println("Result:", result)
}
func add(a, b int) int {
return a + b
}
To debug this application with dlv
:
dlv debug main.go
(dlv) b main.add
(dlv) c
This way, you can step through the add
function and inspect its behavior.
Hash: ba31f601eaf59686e7ba37ba0cd0143636bbfb09bde1dba6a314b4a78eae0152