The runtime package can return the operating system. This is great because someone can just call runtime.GOOS
which will return on of:
const GOOS = `windows`
const GoosAix = 0
const GoosAndroid = 0
const GoosDarwin = 0
const GoosDragonfly = 0
const GoosFreebsd = 0
const GoosHurd = 0
const GoosJs = 0
const GoosLinux = 0
const GoosNacl = 0
const GoosNetbsd = 0
const GoosOpenbsd = 0
const GoosPlan9 = 0
const GoosSolaris = 0
const GoosWindows = 1
const GoosZos = 0
There are some use case where this is not enough and users want to detect the specific version and just the OS family. How to detect the version of the operating system?
From the command line someone can type:
sw_vers
on macOS
ProductName: Mac OS X
ProductVersion: 10.14.5
BuildVersion: 18F132
lsb_release -a
on Ubuntu
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
ver
on Windows
Microsoft Windows [Version 10.0.17134.829]
I guess this can be done by using os/exec
and parsing the output of the commands above, but is there a better way?