[英]How can I detect linux distribution within GOlang program?
I want to write linux distribution independant Golang code.我想编写独立于 linux 发行版的 Golang 代码。 I need detect which linux distribution and need to run distribution specific commands within program.
我需要检测哪个 linux 发行版并需要在程序中运行发行版特定的命令。 Like dpkg in case of Ubuntu and rpm -q in case of RHEL.
像 dpkg 在 Ubuntu 的情况下和 rpm -q 在 RHEL 的情况下。
If you wanted to use a Go module, there's Sysinfo .如果您想使用 Go 模块,可以使用Sysinfo 。
It collects all of the system info (without any dependencies) and gives you JSON which includes what you're looking for.它收集所有系统信息(没有任何依赖项)并为您提供 JSON,其中包括您正在寻找的内容。 Here's a snippet of that data:
这是该数据的一个片段:
"os": {
"name": "CentOS Linux 7 (Core)",
"vendor": "centos",
"version": "7",
"release": "7.2.1511",
"architecture": "amd64"
},
The package containing the executable lsb_release
may or may not be installed on the system.包含可执行文件
lsb_release
的包可能已安装,也可能未安装在系统上。
You could use the file /etc/os-release
, which can be easily parsed with the library go-ini.您可以使用文件
/etc/os-release
,它可以通过库 go-ini 轻松解析。 See example:见示例:
import (
"fmt"
"github.com/go-ini/ini"
)
func ReadOSRelease(configfile string) map[string]string {
cfg, err := ini.Load(configfile)
if err != nil {
log.Fatal("Fail to read file: ", err)
}
ConfigParams := make(map[string]string)
ConfigParams["ID"] = cfg.Section("").Key("ID").String()
return ConfigParams
}
OSInfo := ReadOSRelease("/etc/os-release")
OSRelease := OSInfo["ID"]
fmt.Print(OSRelease)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.