Ben Chuanlong Du's Blog

It is never too late to learn.

Get the Type of OS in Golang

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [4]:
import "fmt"
import "runtime"
import "os/exec"
import "periph.io/x/host/v3/distro"
In [2]:
runtime.GOOS
Out[2]:
linux

List all support OS by Golang.

In [8]:
out, err := exec.Command("bash", "-c", "go tool dist list").Output()
string(out)
Out[8]:
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
freebsd/arm64
illumos/amd64
ios/amd64
ios/arm64
js/wasm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
netbsd/386
netbsd/amd64
netbsd/arm
netbsd/arm64
openbsd/386
openbsd/amd64
openbsd/arm
openbsd/arm64
openbsd/mips64
plan9/386
plan9/amd64
plan9/arm
solaris/amd64
windows/386
windows/amd64
windows/arm
windows/arm64

Get Linux Distribution Information

In [5]:
for k, v := range distro.OSRelease() {
    fmt.Printf("%s: %s\n", k, v)
}
NAME: Ubuntu
VERSION_ID: 22.04
VERSION_CODENAME: jammy
BUG_REPORT_URL: https://bugs.launchpad.net/ubuntu/
HOME_URL: https://www.ubuntu.com/
SUPPORT_URL: https://help.ubuntu.com/
PRIVACY_POLICY_URL: https://www.ubuntu.com/legal/terms-and-policies/privacy-policy
UBUNTU_CODENAME: jammy
PRETTY_NAME: Ubuntu 22.04 LTS
VERSION: 22.04 LTS (Jammy Jellyfish)
ID: ubuntu
ID_LIKE: debian
In [ ]:
 

Comments