Go can introspect its own runtime through the runtime, runtime/debug, runtime/metrics and runtime/pprof packages. They report the Go version and build information, the number of goroutines and CPUs, memory statistics, call stacks and source locations, and let you adjust runtime behaviour such as the garbage collector target. This is different from type reflection with the reflect package, which inspects types and values rather than the running program.
Use these APIs for diagnostics, logging, health endpoints and tuning. They are not meant for application logic.
runtime.GOMAXPROCS(0) reads the current setting without changing it. debug.ReadBuildInfo returns the module path, dependency versions and VCS stamps embedded by go build, which makes it the standard way to expose a version endpoint. From Go 1.25 the default GOMAXPROCS also respects container CPU limits on Linux.
runtime.Caller returns the file and line of a caller, and runtime.Callers with runtime.CallersFrames collects a full stack.
For logging you rarely need this by hand: log/slog records the call site when you set AddSource: true in slog.HandlerOptions. To print the stack of the current goroutine use debug.PrintStack() or debug.Stack().
runtime.ReadMemStats gives a snapshot, but it briefly stops the world. The newer runtime/metrics package is cheaper and more detailed.
Call metrics.All() to list the supported metric names; do not hard-code names you have not checked against your Go version. Exporters such as the Prometheus Go client read these metrics for you.
| Setting | Effect |
|---|---|
debug.SetGCPercent(n) or GOGC | How much the heap may grow before the next collection |
debug.SetMemoryLimit(bytes) or GOMEMLIMIT | Soft memory limit that makes the collector work harder near the limit |
runtime.GOMAXPROCS(n) | Number of OS threads running Go code at once |
runtime.GC() | Forces a collection; use in tests and benchmarks only |
GODEBUG | Switches for runtime and compatibility behaviour |
GOMEMLIMIT was added in Go 1.19 and is the recommended way to keep containers within memory budgets. Setting these values blindly can hurt throughput, so change one value and measure with a profile.
Sending SIGQUIT to a Go process prints all goroutine stacks and exits, which is a quick way to diagnose a hang. Setting GOTRACEBACK=all includes more detail in crash output. For ongoing diagnosis, expose net/http/pprof on a private port.
The word introspection also covers type switches and assertions, which inspect dynamic types of interface values:
Prefer these over reflect when you only need to branch on a few known types.
No. Reflection through reflect examines types and values in your data. Runtime introspection through runtime and runtime/debug examines the state of the executing program, such as goroutines, memory and stacks.
It stops the world briefly, so avoid calling it in a tight loop or on every request. Sample on a timer or use runtime/metrics.
Run go version -m ./binary, or read debug.ReadBuildInfo() in code. Builds in a Git repository embed the commit and time unless you pass -buildvcs=false.
Yes, with runtime.GOMAXPROCS(n), but this is rarely needed. The default matches the available CPUs, and recent releases are aware of container limits.
Go exposes its own runtime state through runtime, runtime/debug, runtime/metrics and runtime/pprof. Use them to report build info, goroutine counts, stacks and memory, and to set limits with GOGC and GOMEMLIMIT. Keep this introspection in diagnostics code, use slog for source locations in logs, and rely on reflect only when you need to inspect your own data types.