一键导入
debug-go
Go debugging best practices, tools, and techniques for effective troubleshooting.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Go debugging best practices, tools, and techniques for effective troubleshooting.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Summarize and condense content effectively
Translate content between languages with context awareness
API testing best practices, tools, and workflows for RESTful API testing.
Code quality standards, metrics, and practices for maintaining clean, maintainable codebases.
Code review best practices, guidelines, and checklists for effective peer reviews.
Technical documentation writing guidelines, standards, and best practices.
基于 SOC 职业分类
| name | debug-go |
| description | Go debugging best practices, tools, and techniques for effective troubleshooting. |
| version | 1.0.0 |
| author | magic |
| license | MIT |
| metadata | {"hermes":{"tags":["go","debugging","troubleshooting","delve"],"category":"software-development"}} |
Comprehensive guide to debugging Go applications using various tools and techniques.
Load this skill when:
# Install
go install github.com/go-delve/delve/cmd/dlv@latest
# Debug current package
dlv debug
# Debug specific test
dlv test ./...
# Debug running process
dlv attach <pid>
# Debug binary
dlv exec ./myapp
# Common commands inside dlv
(dlv) break main.go:42 # Set breakpoint
(dlv) continue # Continue execution
(dlv) next # Next line
(dlv) step # Step into
(dlv) print variable # Print variable
(dlv) goroutines # List goroutines
// Quick debugging - don't forget to remove!
fmt.Printf("DEBUG: value=%v, type=%T\n", value, value)
// Pretty print structs
import "encoding/json"
func prettyPrint(v interface{}) {
b, _ := json.MarshalIndent(v, "", " ")
fmt.Println(string(b))
}
| Feature | Command |
|---|---|
| Set breakpoint | dlv break <file>:<line> |
| Conditional break | dlv cond <breakpoint> <condition> |
| View variables | dlv print <var> |
| Stack trace | dlv stack |
| Goroutines | dlv goroutines |
| Threads | dlv threads |
# Add to your code
import _ "net/http/pprof"
# Start server
go run main.go
# Access profiles
# CPU: http://localhost:6060/debug/pprof/profile
# Memory: http://localhost:6060/debug/pprof/heap
# Goroutines: http://localhost:6060/debug/pprof/goroutine
# Command line
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
func main() {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from panic: %v", r)
debug.PrintStack() // Print stack trace
}
}()
// Your code here
riskyOperation()
}
# Run with race detector
go run -race main.go
go test -race ./...
# Example race condition
var counter int
go func() {
counter++ // Race condition!
}()
// Enable deadlock detection with runtime
import "runtime/debug"
func init() {
debug.SetGCPercent(-1) // Optional: disable GC for debugging
}
// Use go.uber.org/goleak to detect goroutine leaks
// Log HTTP requests
import "net/http/httputil"
func debugTransport() http.RoundTripper {
return &dumpTransport{}
}
type dumpTransport struct{}
func (d *dumpTransport) RoundTrip(req *http.Request) (*http.Response, error) {
dump, _ := httputil.DumpRequestOut(req, true)
fmt.Println(string(dump))
return http.DefaultTransport.RoundTrip(req)
}
import "log/slog"
// Structured logging
slog.Info("User logged in",
"user_id", userID,
"ip", ipAddress,
"duration", time.Since(start))
// Debug level
slog.Debug("Cache miss", "key", cacheKey)
// With context
logger := slog.With("request_id", requestID)
logger.Info("Processing request")
// Development: show all
slog.SetLogLoggerLevel(slog.LevelDebug)
// Production: info and above
slog.SetLogLoggerLevel(slog.LevelInfo)
Problem: File/connection leaks
Solution: Always use defer:
file, err := os.Open("file.txt")
if err != nil {
return err
}
defer file.Close() // Will always run
Problem: Silent failures
Solution: Always check errors:
// Bad
doSomething()
// Good
if err := doSomething(); err != nil {
return fmt.Errorf("failed to do something: %w", err)
}
Problem: Concurrent access to shared state
Solution: Use channels or sync primitives:
// Use mutex
var mu sync.Mutex
var counter int
func increment() {
mu.Lock()
defer mu.Unlock()
counter++
}
After setting up debugging:
dlv debug starts without errorsprintgo test -race passes/debug/pprof/