Code-Memo

Debugging and profiling

Delve (dlv)

Popular debugger for Go.

go install github.com/go-delve/delve/cmd/dlv@latest
dlv debug ./cmd/myapp

Set breakpoints, step, inspect goroutines and locals.

Race detector

go test -race ./...
go run -race .

Catches unsynchronized memory access; expect slower execution.

CPU and memory profiles

go test -cpuprofile=cpu.prof -memprofile=mem.prof ./...
go tool pprof cpu.prof

In pprof interactive mode, top, list, web (with graphviz) help locate hotspots.

Execution trace

go test -trace=trace.out ./...
go tool trace trace.out

Shows scheduler latency, blocking syscalls, and GC pauses.

pprof HTTP endpoints

net/http/pprof registers handlers for live profiling in servers (protect in production with auth/network rules).

fmt.Printf, log.Printf, or structured log/slog with levels — still valid; prefer context-rich logs in services.

go vet and staticcheck

Catch common mistakes before runtime:

go vet ./...
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck ./...