debug package provides debugging facilities for Go programs. It contains subpackages for examining binaries, reading DWARF debugging information, and runtime debugging.
Subpackages
debug/dwarf
Access to DWARF debugging information from executable files.debug/elf
Access to ELF (Executable and Linkable Format) object files.debug/macho
Access to Mach-O object files (macOS and iOS).debug/pe
Access to PE (Portable Executable) files (Windows).debug/plan9obj
Access to Plan 9 a.out object files.debug/buildinfo
Read build information embedded in Go binaries.Runtime Debugging
runtime/debug Package
Build Info at Runtime
Practical Examples
Crash Recovery with Stack Trace
Memory Profiling
Extract Version Information
Binary Analysis
Common Use Cases
- Binary inspection: Examine compiled binaries and their structure
- Debugging information: Extract debug symbols and line information
- Dependency analysis: List dependencies of a compiled binary
- Version tracking: Embed and extract version information
- Memory debugging: Track memory usage and garbage collection
- Crash analysis: Capture and analyze stack traces
- Build validation: Verify build settings and dependencies
Best Practices
- Always close file handles - Use
defer f.Close()when opening binaries - Handle format variations - Different platforms use different executable formats
- Check error returns - Binary parsing can fail in various ways
- Use runtime/debug for production - Safer than debug/elf etc. at runtime
- Embed build info - Use
-ldflagsto embed version information during build