osdetect

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 5 Imported by: 0

README

osdetect

Lightweight, dependency-free OS detection for Go.

osdetect gives you structured, reliable information about the operating system your program is running on — distro, version, kernel/build, and architecture — without shelling out or pulling in third-party packages.

info := osdetect.GetInfo()
fmt.Println(info.Name, info.Version)
// Ubuntu 22.04   (on Linux)
// Windows 11 Pro  23H2   (on Windows)

Features

  • 🪶 Zero dependencies — pure standard library, on every platform.
  • 🐧 Linux — parses /etc/os-release and the kernel build string.
  • 🪟 Windows — reads the real registry (CurrentVersion key) directly via syscall, no shell-outs to wmic/systeminfo.
  • 🧩 Uniform Info struct — same shape regardless of platform, with an optional platform-specific detail struct (LinuxInfo / WindowsInfo).
  • 🗃️ Raw data included — every unparsed key/value pair is preserved in Raw, so nothing is thrown away even if a field isn't modeled yet.

Install

go get github.com/amirarsalan/osdetect

Requires Go 1.26+.


Usage

package main

import (
	"fmt"

	"github.com/amirarsalan/osdetect"
)

func main() {
	info := osdetect.GetInfo()

	fmt.Printf("Family:  %s\n", info.Family)
	fmt.Printf("Arch:    %s\n", info.Arch)
	fmt.Printf("Name:    %s\n", info.Name)
	fmt.Printf("Version: %s\n", info.Version)
	fmt.Printf("Kernel:  %s\n", info.Kernel)

	switch info.Family {
	case osdetect.Linux:
		fmt.Printf("Distro: %s (like: %s)\n", info.Linux.Distro, info.Linux.DistroLike)
	case osdetect.Windows:
		fmt.Printf("Edition: %s (server: %v)\n", info.Windows.Edition, info.Windows.IsServer)
	}
}
Example output

Linux (Ubuntu 22.04):

Family:  linux
Arch:    amd64
Name:    ubuntu
Version: 22.04
Kernel:  5.15.0-91-generic
Distro:  ubuntu (like: debian)

Windows 11 Pro:

Family:  windows
Arch:    amd64
Name:    Windows 11 Pro
Version: 23H2
Kernel:  22631.2861
Edition: Professional (server: false)

API reference

GetInfo() *Info

Platform-appropriate implementation selected at compile time via Go build tags (osinfo_linux.go, osinfo_windows.go). Returns a fully populated *Info for the current OS.

Info
Field Type Description
Family Family "linux", "windows", "darwin", or "unknown"
Arch string runtime.GOARCH"amd64", "arm64", "386", ...
Name string Human-friendly name, e.g. "Ubuntu", "Windows 11 Pro"
Version string Best "primary" version string, e.g. "22.04", "23H2"
Kernel string Kernel/build string, e.g. "5.15.0-91-generic", "22631.2861"
Linux *LinuxInfo Populated only when Family == Linux
Windows *WindowsInfo Populated only when Family == Windows
Raw map[string]string Every unparsed key/value read from the source, for debugging or fields not yet modeled
LinuxInfo
Field Description
Distro ID field, e.g. "ubuntu", "debian", "fedora"
DistroLike ID_LIKE field, e.g. "debian" for Ubuntu/Mint
PrettyName PRETTY_NAME, e.g. "Ubuntu 22.04.3 LTS"
VersionID VERSION_ID, e.g. "22.04"
Codename VERSION_CODENAME, e.g. "jammy"
Kernel uname -r equivalent
WindowsInfo
Field Description
Edition e.g. "Pro", "Home", "Enterprise", "Server"
Build e.g. "22631.2861" (CurrentBuild + UBR)
DisplayVersion e.g. "23H2"
ProductName Full registry ProductName string
IsServer true for Windows Server editions

How it works

Platform Source
Linux /etc/os-release + /proc/sys/kernel/osrelease
Windows HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion via the standard syscall package

No subprocesses, no CGO, no third-party modules — just the pieces of the standard library that already know how to talk to the OS.


Roadmap

  • macOS support (sw_vers / /System/Library/CoreServices/SystemVersion.plist)
  • BSD family support

Contributions welcome — open a PR or an issue.


License

MIT

Documentation

Overview

Package osdetect provides utilities for detecting operating system information. This file contains the Linux-specific implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetKernelVersion

func GetKernelVersion() (string, error)

GetKernelVersion returns the Linux kernel version string (e.g., "6.8.0-45-generic"). It reads this information directly from /proc/sys/kernel/osrelease.

Types

type Family

type Family string

Family represents the broad category of the operating system.

const (
	// Linux represents any Linux-based operating system.
	Linux Family = "linux"
	// Windows represents Microsoft Windows operating systems.
	Windows Family = "windows"
	// Darwin represents Apple macOS operating systems.
	Darwin Family = "darwin"
	// Unknown represents an operating system that could not be identified.
	Unknown Family = "unknown"
)

type Info

type Info struct {
	// Family is the broad OS category (linux, windows, darwin, unknown).
	Family Family

	// Arch is the architecture of the OS, matching runtime.GOARCH (e.g., "amd64", "arm64", "386").
	Arch string

	// Name is a human-friendly name of the OS (e.g., "Ubuntu", "Windows 11 Pro").
	Name string

	// Version is the best "primary" version string available (e.g., "22.04", "10.0.22631").
	Version string

	// Kernel is the kernel or build version, if available (e.g., "5.15.0-91-generic").
	Kernel string

	// Linux contains detailed information if Family is Linux.
	// It is nil for other OS families.
	Linux *LinuxInfo `json:",omitempty"`

	// Windows contains detailed information if Family is Windows.
	// It is nil for other OS families.
	Windows *WindowsInfo `json:",omitempty"`

	// Raw holds unparsed key/value pairs read from the source (e.g., /etc/os-release or registry).
	// This is useful for debugging or accessing fields not modeled by this library.
	Raw map[string]string `json:",omitempty"`
}

Info encapsulates the detected information about the operating system. It provides a high-level summary and detailed, OS-specific information.

func GetInfo

func GetInfo() *Info

GetInfo retrieves detailed information about the current Linux system. It parses /etc/os-release for distribution details and reads the kernel version. If it fails to parse the release info or read the kernel version, it will panic.

type LinuxInfo

type LinuxInfo struct {
	// Distro is the machine-readable ID field (e.g., "ubuntu", "debian", "fedora", "arch").
	Distro string

	// DistroLike represents the ID_LIKE field, indicating closely related distributions (e.g., "debian" for Ubuntu).
	DistroLike string

	// PrettyName is a human-readable string identifying the OS (e.g., "Ubuntu 22.04.3 LTS").
	PrettyName string

	// VersionID is a machine-readable string identifying the OS version (e.g., "22.04").
	VersionID string

	// Codename is a machine-readable string identifying the OS version codename (e.g., "jammy").
	Codename string

	// Kernel is the kernel release string, typically from uname -r or /proc/sys/kernel/osrelease.
	Kernel string
}

LinuxInfo holds distribution-specific details for Linux systems. This information is typically sourced from /etc/os-release.

type OSReleaseInfo

type OSReleaseInfo map[string]string

OSReleaseInfo represents the parsed key-value pairs from an os-release file.

func ParseRelease

func ParseRelease() (OSReleaseInfo, error)

ParseRelease reads and parses the /etc/os-release file. It extracts key-value pairs according to the standard os-release format and returns them as an OSReleaseInfo map.

func (OSReleaseInfo) GetField

func (o OSReleaseInfo) GetField(key, defaultValue string) string

GetField returns the value of the specified key. If the key is not found or its value is empty, it returns the defaultValue.

func (OSReleaseInfo) GetID

func (o OSReleaseInfo) GetID() string

GetID returns the ID field, which is a machine-readable string identifying the distribution (e.g., "ubuntu", "debian", "fedora").

func (OSReleaseInfo) GetPrettyName

func (o OSReleaseInfo) GetPrettyName() string

GetPrettyName returns the PRETTY_NAME field, which is typically the most user-friendly name of the distribution (e.g., "Ubuntu 22.04.3 LTS"). If PRETTY_NAME is empty, it falls back to the NAME field.

func (OSReleaseInfo) GetVersion

func (o OSReleaseInfo) GetVersion() string

GetVersion returns the value of the VERSION field.

func (OSReleaseInfo) GetVersionID

func (o OSReleaseInfo) GetVersionID() string

GetVersionID returns the VERSION_ID field, which is a machine-readable string identifying the operating system version (e.g., "22.04", "36").

type WindowsInfo

type WindowsInfo struct {
	// Edition is the specific edition of Windows (e.g., "Pro", "Home", "Enterprise", "ServerDatacenter").
	Edition string

	// Build is the Windows build number (e.g., "22631").
	Build string

	// DisplayVersion is the user-facing version name (e.g., "23H2", "22H2").
	DisplayVersion string

	// ProductName is the full product name string as found in the registry (e.g., "Windows 10 Pro").
	ProductName string

	// IsServer indicates whether the operating system is a Windows Server edition.
	IsServer bool
}

WindowsInfo holds detailed information specific to Windows systems. This information is often sourced from the Windows Registry or WMI.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL