This repository provides Go packages to retrieve CPU information and top processes on macOS.
The project is divided into two packages:
cpuinfo
: Provides functions to get CPU cores and usage information.processes
: Provides functions to retrieve the top processes by CPU usage.
Retrieves the number of CPU cores and the current CPU usage.
Returns:
int
: Number of CPU cores.float64
: Total CPU usage (user + system).
Retrieves the number of CPU cores using runtime.NumCPU()
and as a fallback, the sysctl
command.
Returns:
int
: Number of CPU cores.
Retrieves the current CPU usage by executing the top
command.
Returns:
float64
: Total CPU usage (user + system).error
: Error encountered during execution, if any.
package main
import (
"fmt"
"cpuinfo"
)
func main() {
cores, usage := cpuinfo.GetDarwinCPUInfo()
fmt.Printf("CPU Cores: %d\n", cores)
fmt.Printf("CPU Usage: %.2f%%\n", usage)
}