Skip to main content
Returns detailed information about the CPU(s) in the system, including vendor, model, frequency, and supported features.

Function Signatures

func Info() ([]InfoStat, error)
func InfoWithContext(ctx context.Context) ([]InfoStat, error)

Parameters

ctx
context.Context
Context for cancellation and timeout control (WithContext variant only).

Return Value

[]InfoStat
[]InfoStat
Array of CPU information structures. On Linux, returns one entry per physical thread (including hyperthreading). On other platforms, behavior may vary.
error
error
Error if CPU information could not be retrieved, nil on success.

InfoStat Structure

type InfoStat struct {
    CPU        int32    `json:"cpu"`
    VendorID   string   `json:"vendorId"`
    Family     string   `json:"family"`
    Model      string   `json:"model"`
    Stepping   int32    `json:"stepping"`
    PhysicalID string   `json:"physicalId"`
    CoreID     string   `json:"coreId"`
    Cores      int32    `json:"cores"`
    ModelName  string   `json:"modelName"`
    Mhz        float64  `json:"mhz"`
    CacheSize  int32    `json:"cacheSize"`
    Flags      []string `json:"flags"`
    Microcode  string   `json:"microcode"`
}

Fields

CPU
int32
Logical CPU number (0-indexed).
VendorID
string
CPU vendor identifier. Common values:
  • "GenuineIntel" - Intel CPUs
  • "AuthenticAMD" - AMD CPUs
  • "ARM" - ARM processors
Family
string
CPU family identifier.
Model
string
CPU model identifier.
Stepping
int32
CPU stepping (revision) number.
PhysicalID
string
Physical processor socket ID. CPUs in the same socket share this ID.
CoreID
string
Core ID within the physical processor. Hyperthreaded cores share the same CoreID.
Cores
int32
Number of cores in this physical processor. On Linux with hyperthreading, this is typically 1 per thread.
ModelName
string
Human-readable CPU model name, e.g., "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz".
Mhz
float64
Current or maximum CPU frequency in MHz. On Linux, reports the maximum frequency from cpuinfo_max_freq.
CacheSize
int32
CPU cache size in KB.
Flags
[]string
Array of CPU feature flags (e.g., "sse", "sse2", "avx", "avx2").
Microcode
string
CPU microcode version.

Examples

Get Basic CPU Information

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
)

func main() {
    info, err := cpu.Info()
    if err != nil {
        panic(err)
    }
    
    if len(info) > 0 {
        cpu := info[0]
        fmt.Printf("CPU: %s\n", cpu.ModelName)
        fmt.Printf("Vendor: %s\n", cpu.VendorID)
        fmt.Printf("Cores: %d\n", cpu.Cores)
        fmt.Printf("Frequency: %.2f MHz\n", cpu.Mhz)
    }
}

List All CPU Cores

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
)

func main() {
    info, err := cpu.Info()
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Found %d CPU cores:\n", len(info))
    for i, cpu := range info {
        fmt.Printf("CPU%d: %s (Physical ID: %s, Core ID: %s)\n",
            i, cpu.ModelName, cpu.PhysicalID, cpu.CoreID)
    }
}

Check CPU Features

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
)

func hasFeature(flags []string, feature string) bool {
    for _, f := range flags {
        if f == feature {
            return true
        }
    }
    return false
}

func main() {
    info, err := cpu.Info()
    if err != nil {
        panic(err)
    }
    
    if len(info) > 0 {
        flags := info[0].Flags
        fmt.Printf("SSE2 support: %v\n", hasFeature(flags, "sse2"))
        fmt.Printf("AVX support: %v\n", hasFeature(flags, "avx"))
        fmt.Printf("AVX2 support: %v\n", hasFeature(flags, "avx2"))
        fmt.Printf("AES-NI support: %v\n", hasFeature(flags, "aes"))
    }
}

Using Context with Timeout

package main

import (
    "context"
    "fmt"
    "time"
    "github.com/shirou/gopsutil/v4/cpu"
)

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    
    info, err := cpu.InfoWithContext(ctx)
    if err != nil {
        if ctx.Err() == context.DeadlineExceeded {
            fmt.Println("Operation timed out")
        } else {
            panic(err)
        }
        return
    }
    
    fmt.Printf("Retrieved info for %d CPUs\n", len(info))
}

Export to JSON

package main

import (
    "encoding/json"
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
)

func main() {
    info, err := cpu.Info()
    if err != nil {
        panic(err)
    }
    
    // InfoStat has JSON tags for all fields
    jsonData, err := json.MarshalIndent(info, "", "  ")
    if err != nil {
        panic(err)
    }
    
    fmt.Println(string(jsonData))
}

Count Physical Sockets and Cores

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
)

func main() {
    info, err := cpu.Info()
    if err != nil {
        panic(err)
    }
    
    sockets := make(map[string]bool)
    cores := make(map[string]bool)
    
    for _, cpu := range info {
        sockets[cpu.PhysicalID] = true
        cores[cpu.PhysicalID+":"+cpu.CoreID] = true
    }
    
    fmt.Printf("Physical sockets: %d\n", len(sockets))
    fmt.Printf("Physical cores: %d\n", len(cores))
    fmt.Printf("Logical processors: %d\n", len(info))
}

Notes

  • The Info() function is a convenience wrapper that calls InfoWithContext() with context.Background()
  • On Linux, this function returns one InfoStat per physical thread (including hyperthreading)
    • For example, a dual-socket system with 8 cores per socket and hyperthreading will return 32 entries
    • Each entry will have Cores: 1 in this case
  • On Windows and macOS, the behavior may differ
  • Not all fields are available on all platforms:
    • Microcode is primarily available on Linux
    • Flags availability varies by platform
  • The Mhz field on Linux reports the maximum frequency, not the current frequency
  • Use Counts() to get a simple count of logical or physical cores

Build docs developers (and LLMs) love