Overview
gopsutil is designed as a pure Go port of Python’s psutil library, providing system and process information across multiple operating systems without using cgo. This architecture ensures portability, ease of deployment, and consistent behavior across platforms.Core Design Principles
Pure Go Implementation
All functionality is implemented without cgo by porting C structs to Go structs, ensuring easy cross-compilation and deployment.
Cross-Platform Support
Supports Linux, Windows, macOS, FreeBSD, OpenBSD, Solaris, and AIX with consistent APIs across platforms.
Context-Aware
All functions support Go’s context.Context for cancellation, timeouts, and passing configuration.
Minimal Dependencies
Uses only Go standard library and platform-specific syscalls to minimize external dependencies.
Package Structure
gopsutil is organized into focused packages, each handling a specific aspect of system information:cpu - CPU Information
cpu - CPU Information
Provides CPU times, counts, percentages, and detailed CPU information including vendor, model, and features.
mem - Memory Information
mem - Memory Information
Retrieves virtual and swap memory statistics including total, available, used, and percentages.
disk - Disk Information
disk - Disk Information
Handles disk partitions, usage, and I/O counters for storage devices.
host - Host Information
host - Host Information
Provides system-level information including hostname, uptime, boot time, platform, and virtualization details.
process - Process Management
process - Process Management
Offers comprehensive process information and control including CPU, memory, I/O, and process lifecycle.
net - Network Information
net - Network Information
Retrieves network interface statistics, connections, and protocol information.
load - System Load
load - System Load
Provides system load averages (1, 5, and 15 minutes) on supported platforms.
Platform-Specific Implementations
Cross-Platform Abstraction
Each package defines common interfaces and types that work across all platforms.
Platform-Specific Files
Implementation details are handled in platform-specific files using build tags.
Platform-Specific Extensions (Ex Structs)
Starting from v4.24.5, gopsutil provides
Ex structs for accessing platform-specific information that isn’t available across all operating systems.Ex structures allow you to access this data:
The
Ex pattern makes it explicit that you’re using platform-specific functionality, helping you write more maintainable code when cross-platform compatibility isn’t required.Data Flow Architecture
The typical data flow in gopsutil follows this pattern:Error Handling
gopsutil uses standard Go error handling patterns:Struct Serialization
All major structs in gopsutil implementString() methods that return JSON representations:
Next Steps
Context Usage
Learn how to use context for configuration and control
Environment Variables
Configure system paths for containerized environments
Caching
Understand caching strategies for performance
API Reference
Explore detailed API documentation