The standard tools for debugging and profiling Go programs are Delve for interactive debugging, pprof for CPU, memory and goroutine profiling, go tool trace for execution traces, and the race detector for data races. All profiling tools except Delve ship with Go.
Use a debugger to understand why one execution behaves wrongly, and a profiler to find where a program spends time or memory across many executions.
Delve (dlv) is the Go debugger and the engine behind the Go extension for VS Code and GoLand. It understands goroutines, channels and Go data types, which general debuggers do not handle well.
Inside the prompt you can use these commands:
You can also attach to a running process with dlv attach <pid> and debug tests with dlv test. Compile with -gcflags="all=-N -l" to disable optimisations and inlining when variables appear as optimised out.
GDB can debug Go binaries built by the standard gc compiler, not only gccgo, and the Go runtime ships helper scripts for it. Its support for goroutines and Go types is limited, so the Go documentation recommends Delve instead. Use GDB only when you must debug mixed Go and C code with cgo.
pprof samples a running program and reports where CPU time, memory and blocking occur. Expose it in a service with a side-effect import:
Bind this to localhost or an internal port only, since the endpoints expose internal details. Then collect and view profiles:
The -http option opens a web UI with a flame graph, so the older go-torch tool is no longer needed and is no longer maintained. Available profiles include CPU, heap, allocs, goroutine, block and mutex; the block and mutex profiles must be enabled with runtime.SetBlockProfileRate and runtime.SetMutexProfileFraction.
Benchmarks can write profiles directly:
Use benchstat to compare benchmark runs statistically instead of trusting a single result. Since Go 1.21, profile-guided optimisation lets the compiler use a CPU profile (a file named default.pgo in the main package) to optimise hot paths.
runtime/trace records scheduler, garbage collector and goroutine events with timestamps, which helps with latency and concurrency problems that sampling profiles miss.
For data races, build or test with -race:
The race detector slows the program and uses more memory, so use it in tests and staging rather than in production.
runtime/debug offers debug.PrintStack, debug.Stack, debug.SetGCPercent and debug.ReadBuildInfo; it does not produce heap dumps for analysis.runtime.ReadMemStats and the runtime/metrics package expose runtime statistics.GODEBUG=gctrace=1 prints garbage collector activity, and GOTRACEBACK=all prints all goroutine stacks on a crash.SIGQUIT to a Go process dumps all goroutine stacks, which is a quick way to find a deadlock.Continuous profiling services such as Pyroscope or Parca collect pprof data from production over time, so you can compare behaviour before and after a release.
Debugging (Delve) inspects one execution step by step to find a logic bug. Profiling (pprof) samples many executions to find slow or memory-heavy code.
Only on an internal address or behind authentication. The endpoints reveal stack traces and memory contents and can add load while profiling.
Compare /debug/pprof/goroutine?debug=2 output over time, or look at the goroutine profile in pprof. Goroutines stuck on the same line, usually a channel operation, point to the leak.
No. go tool pprof -http includes a flame graph view, and go-torch is obsolete.
Use Delve to step through code, pprof for CPU, heap and goroutine analysis, go tool trace for scheduling and latency, and -race for data races. Expose profiling endpoints only on internal addresses, and measure with benchmarks before and after any optimisation.