API Reference

gopsutil

Cross-platform Go library for reading system metrics such as CPU, memory, disk and process info.

Official Documentation ↗
CompatibilityThis documentation covers version gopsutil v4 / Go 1.20+. Check the official docs for the latest API surface.

Installation

gogo get github.com/shirou/gopsutil/v4

Methods

Overview

gopsutil exposes system information through Go packages for CPU, memory, disk, network, host and process metrics. It enables monitoring, health checks and diagnostics without cgo on most platforms.

Core Methods

Call functions in subpackages such as cpu, mem, disk, net and process. Percent and counters need two samples to compute rates, and NewProcess reads per-process stats by pid.

Usage Notes

Sample on a fixed interval, compute rates from counter deltas, and guard against platform-specific missing fields. Keep sampling cheap so it does not distort the measurements.

Common Pitfalls

Reading raw counters as instantaneous values is wrong; they are cumulative. Calling CPU percent with a blocking interval in a request path stalls, so prefer the non-blocking form. Some metrics are unavailable on certain operating systems.

API reference evidence

Fetched from official sources on 2026-11-08T19:09:00+08:00 via WebFetch https://pkg.go.dev/github.com/shirou/gopsutil/v4/cpu;PowerShell Invoke-WebRequest https://raw.githubusercontent.com/shirou/gopsutil/v4.26.8/mem/mem.go(未命中 VirtualMemory);PowerShell Invoke-WebRequest https://raw.githubusercontent.com/shirou/gopsutil/v4.26.8/process/process.go.

FAQ

Is it cross-platform consistent?
The library abstracts many platforms but some fields are unavailable or partially populated on certain OSes. Check for zero values and document platform assumptions.
What interval should I use for CPU percent?
Pass 0 to compare against the last call, or a positive duration to block and sample over that window. Non-blocking sampling needs two calls to be meaningful.
How do I read disk or network metrics?
Use the disk and net subpackages for usage and IO counters. Counters are cumulative, so compute rates from differences over time rather than reading single values.