HowtoGo
Home / Standard Library / The runtime Package
Standard Library

The runtime Package

runtime is the machinery underneath every Go program: the scheduler, the garbage collector, the stack. Most of it you never call, and a few functions are worth knowing.

Examples

Four facts about the binary and the host, available without importing anything else.

fmt.Println(runtime.GOOS, runtime.GOARCH)
fmt.Println("cpus:", runtime.NumCPU())
fmt.Println(runtime.Version())
Output
linux amd64
cpus: 8
go1.26.1
FunctionDescription
GOOS
runtime.GOOS == "linux"
The operating system the binary was built for: linux, darwin, windows.
GOARCH
runtime.GOARCH // "amd64"
The architecture the binary was built for: amd64, arm64.
NumCPU() int
runtime.NumCPU() // 8
How many hardware threads the machine reports.
Version() string
runtime.Version() // "go1.26.1"
The Go version the binary was built with.
NumGoroutine() int
How many goroutines exist right now.
Gosched()
runtime.Gosched()
Hands the processor to another goroutine and comes back.
GOMAXPROCS(n int) int
runtime.GOMAXPROCS(0) // read without setting
Sets how many goroutines may run at once, and returns the previous value.
ReadMemStats(m *MemStats)
runtime.ReadMemStats(&m)
Fills a MemStats with the current heap and GC numbers.
MemStats
The heap numbers: Alloc is live bytes, TotalAlloc is everything ever allocated, Sys is what was taken from the OS, NumGC counts collections.
GC()
runtime.GC()
Runs a garbage collection and waits for it.
SetFinalizer(obj any, finalizer any)
runtime.SetFinalizer(f, cleanup)
Runs a function when the object becomes unreachable.
Caller(skip int) (pc uintptr, file string, line int, ok bool)
_, file, line, _ := runtime.Caller(1)
The file and line of the caller, skip frames up.
FuncForPC(pc uintptr) *Func
runtime.FuncForPC(pc).Name()
The function containing a program counter, for its name.
Stack(buf []byte, all bool) int
runtime.Stack(buf, true)
Writes a stack trace into buf, optionally for every goroutine.
Most of this is not for you

The scheduler and the collector are tuned for the general case and are usually better at their jobs than a guess made from outside. Gosched, GC and SetFinalizer in particular tend to appear in code that wanted a channel, a benchmark, or a defer.

The parts that earn their place every day are the read-only ones: which platform this is, how many goroutines exist, how much memory is live, and where a call came from.