You might find invaluable during Go debugging sessions:
- Breakpoint Commands:
b or break main.go:34 - Set breakpoint at line 34
bp or breakpoints - List all breakpoints
clear 1 - Remove breakpoint #1
clearall - Remove all breakpoints
condition <bp> <expr> - Set breakpoint condition
- Execution Control:
c or continue - Run until breakpoint
n or next - Step over (execute current line)
s or step - Step into function
stepout - Step out of current function
restart - Restart the debugging session
- Variable Inspection:
p or print <var> - Print variable value
whatis <var> - Show variable type
vars - Display local variables
locals - Show all local variables
args - Show function arguments
examples
# Print a variable value
p myVariable
p users[0].Name
# Print struct with all fields
print %x myStruct
# Show type information
whatis myVariable
# Show all local variables in current function
locals
# Print slice/array with 4 elements per line
p -l 4 mySlice
- Goroutine Management:
goroutines - List all goroutines
goroutine - Show current goroutine
gr <id> - Switch to specific goroutine
examples
# List all goroutines with their states
goroutines
# Show details of current goroutine
goroutine
# Switch to goroutine #12
gr 12
# Show goroutines with specific filter
goroutines -t # Show only running goroutines
goroutines -s # Show start location
- Stack Manipulation:
bt or stack - Print stack trace
frame <n> - Select frame by number
up - Move up one frame
down - Move down one frame
- Data Structure Navigation:
set <variable> = <value> - Change variable value
examinemem <addr> - Examine memory
display - Print value of an expression each time program stops
- Function-related:
funcs <pattern> - List functions matching pattern
list - Show source code around current point
disassemble - Disassemble current function
examples
# List all functions containing "handler"
funcs handler
# List all methods of a struct
funcs MyStruct
# Show current function source
list
# Show specific lines of code
list 15:20
- Process Control:
exit or quit - Exit debugger
restart - Restart process
on <event> <cmd> - Execute command when event occurs
Particularly useful advanced techniques:
# Conditional breakpoints
break main.go:34 -c "someVar == 5"
# Break on function
break MyFunction
# Watch variables
watch someVariable
# Print with format
p -f hex someValue # Print in hexadecimal
# Show stack trace with full argument values
stack -full
# Show all threads
threads
# Memory statistics
memstats
# Check current package/function
frame
# Register values
regs
# Set dynamic breakpoint on function entry
b MyFunction
# Break when variable changes
watch myVariable
# Break with condition
b main.go:35 -c "err != nil"
# Print every time a variable changes
display myVariable
# Start debugging
dlv debug ./cmd/myapp/main.go
# Set initial breakpoint
b main.main
# Run until breakpoint
continue (or c)
# Step through code
next (or n) # Step over
step (or s) # Step into
stepout # Step out of function
# Examine state
bt # Show stack trace
p variableName # Print variable
whatis v # Show variable type
Pro Tips:
0. Use ctrl+d
to exit delve
- You can use
help <command>
to get detailed information about any command - Use
config
to set various debugger configurations - The
source
command can load a file containing delve commands - Use
trace
to set tracepoints that don't stop execution but print information - The
on
command is powerful for automating debugging tasks - Up/down arrow keys navigate command history
And finally Use
clear
to remove breakpoints when they're no longer needed.
Happy Debugging!