Skip to main content
The Info() function returns detailed information about the host system including hostname, OS, platform, kernel version, virtualization details, and more.

Function Signature

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

Return Value

Returns a pointer to InfoStat struct containing:
Hostname
string
System hostname
Uptime
uint64
System uptime in seconds
BootTime
uint64
System boot time in seconds since Unix epoch
Procs
uint64
Number of currently running processes
OS
string
Operating system (e.g., “linux”, “freebsd”, “darwin”, “windows”)
Platform
string
Platform distribution (e.g., “ubuntu”, “centos”, “debian”)
PlatformFamily
string
Platform family (e.g., “debian”, “rhel”, “arch”)
PlatformVersion
string
Version of the complete operating system
KernelVersion
string
Kernel version string
KernelArch
string
Native CPU architecture (equivalent to uname -m)
VirtualizationSystem
string
Virtualization system if running in a VM (e.g., “kvm”, “xen”, “vmware”)
VirtualizationRole
string
Either “guest” or “host” indicating virtualization role
HostID
string
Unique host identifier (usually UUID)

Usage Examples

Basic Usage

package main

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

func main() {
    info, err := host.Info()
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Hostname: %s\n", info.Hostname)
    fmt.Printf("OS: %s\n", info.OS)
    fmt.Printf("Platform: %s %s\n", info.Platform, info.PlatformVersion)
    fmt.Printf("Kernel: %s\n", info.KernelVersion)
    fmt.Printf("Architecture: %s\n", info.KernelArch)
    fmt.Printf("Uptime: %d seconds\n", info.Uptime)
}

With Context and Timeout

package main

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

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    
    info, err := host.InfoWithContext(ctx)
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("Host ID: %s\n", info.HostID)
    fmt.Printf("Running processes: %d\n", info.Procs)
}

Checking Virtualization

package main

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

func main() {
    info, err := host.Info()
    if err != nil {
        panic(err)
    }
    
    if info.VirtualizationSystem != "" {
        fmt.Printf("Running in %s as %s\n", 
            info.VirtualizationSystem, 
            info.VirtualizationRole)
    } else {
        fmt.Println("Running on bare metal")
    }
}

JSON Output

package main

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

func main() {
    info, err := host.Info()
    if err != nil {
        panic(err)
    }
    
    // InfoStat has a String() method that returns JSON
    fmt.Println(info.String())
    
    // Or use json.Marshal for pretty printing
    data, _ := json.MarshalIndent(info, "", "  ")
    fmt.Println(string(data))
}

Error Handling

The function may return errors when:
  • Unable to access system information files (permission issues)
  • Platform-specific system calls fail
  • Context deadline is exceeded (when using InfoWithContext)
Some fields may be empty on certain platforms if the information is not available or not implemented for that OS.

Platform Notes

Reads from /proc/sys/kernel, /etc/os-release, and other system files. Requires read access to these locations.

BootTime()

Get only the boot time

Users()

Get logged-in users

Build docs developers (and LLMs) love