I want to write linux distribution independant Golang code. I need detect which linux distribution and need to run distribution specific commands within program. Like dpkg in case of Ubuntu and rpm -q in case of RHEL.
How can I detect linux distribution within GOlang program?
5.7k Views Asked by avishkar At
4
There are 4 best solutions below
0

If you wanted to use a Go module, there's Sysinfo.
It collects all of the system info (without any dependencies) and gives you JSON which includes what you're looking for. Here's a snippet of that data:
"os": {
"name": "CentOS Linux 7 (Core)",
"vendor": "centos",
"version": "7",
"release": "7.2.1511",
"architecture": "amd64"
},
0

There's a convenient command line tool backed by a go library here: dekobon/distro-detect
Here's how the library is called in the main function of the command line
linux.FileSystemRoot = fsRoot
distro := linux.DiscoverDistro()
It parses both the os-release file and the lsb-release file declarations.
The package containing the executable
lsb_release
may or may not be installed on the system.You could use the file
/etc/os-release
, which can be easily parsed with the library go-ini. See example: