Last active
April 9, 2023 05:50
-
-
Save flxxyz/ae3ef071dc4ffb0c55daedc7f0740611 to your computer and use it in GitHub Desktop.
obtain the system version number(windows, linux, darwin or macos)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// golang 获取系统版本号 (windows, linux, darwin or macos) | |
// golang obtain the system version number (windows, linux, darwin or macos) | |
package main | |
import ( | |
"bytes" | |
"io/ioutil" | |
"log" | |
"os" | |
"os/exec" | |
"regexp" | |
"runtime" | |
"strconv" | |
) | |
type OS struct { | |
Name string | |
ProductName string | |
ProductVersion string | |
Arch string | |
} | |
func NewOS() (os *OS) { | |
os = &OS{ | |
Name: runtime.GOOS, | |
ProductName: "unknown", | |
ProductVersion: "unknown", | |
Arch: runtime.GOARCH, | |
} | |
switch os.Name { | |
case "windows": | |
os.ProductName, os.ProductVersion = GetWindowsVersion() | |
case "linux": | |
os.ProductName, os.ProductVersion = GetLinuxVersion() | |
case "darwin": | |
os.ProductName, os.ProductVersion = GetDarwinVersion() | |
} | |
return | |
} | |
// GetWindowsVersion 获取windows版本号 | |
// GetWindowsVersion Obtain the version number of Windows | |
func GetWindowsVersion() (name, version string) { | |
// TODO:我现在还不了解如何区分桌面版与服务器版 | |
// TODO: I don't currently know how to distinguish between desktop and server editions | |
name = "unknown" | |
versionNumbers := map[string]string{ | |
`5\.0`: "2000", | |
`5\.1`: "XP", | |
`5\.2`: "Server 2003", | |
`6\.0`: "Server 2008", | |
`6\.1`: "Server 2008 R2", | |
`6\.2`: "Server 2012", | |
`6\.3`: "Server 2012 R2", | |
`10\.0`: "10", | |
} | |
//win10VersionNumbers := map[string]string{ | |
// `10\.0\.14300`: "Server 2016", | |
// `10\.0\.14393`: "Server 2016", | |
// `10\.0\.16299`: "Server 2016", | |
// `10\.0\.17134`: "Server 2016", | |
// `10\.0\.17677`: "Server 2019", | |
// `10\.0\.17763`: "Server 2019", | |
// `10\.0\.18362`: "Server 2019", | |
// `10\.0\.18363`: "Server 2019", | |
//} | |
cmd := exec.Command("cmd.exe") | |
out, _ := cmd.StdoutPipe() | |
buffer := bytes.NewBuffer(make([]byte, 0)) | |
cmd.Start() | |
buffer.ReadFrom(out) | |
str, _ := buffer.ReadString(']') | |
cmd.Wait() | |
for key, _ := range versionNumbers { | |
re := regexp.MustCompile(`Microsoft Windows \[[\s\S]* ` + key + `\.([0-9]+).?[0-9]*\]`) | |
if re.MatchString(str) { | |
if versionNumbers[key] != "10" { | |
version = versionNumbers[key] | |
} else { | |
versionNumber := re.FindStringSubmatch(str) | |
if len(versionNumber) > 1 { | |
val, _ := strconv.Atoi(versionNumber[1]) | |
if val <= 17134 { | |
version = "Server 2016" | |
} else { | |
version = "Server 2019" | |
} | |
} | |
} | |
return | |
} | |
} | |
return | |
} | |
// GetLinuxVersion 获取linux版本号 | |
// GetLinuxVersion Obtain the version number of Linux | |
func GetLinuxVersion() (name, version string) { | |
if ok, _ := PathExists("/etc/os-release"); ok { | |
cmd := exec.Command("cat", "/etc/os-release") | |
stdout, _ := cmd.StdoutPipe() | |
cmd.Start() | |
content, err := ioutil.ReadAll(stdout) | |
if err == nil { | |
id := regexp.MustCompile(`\nID="?(.*?)"?\n`).FindStringSubmatch(string(content)) | |
if len(id) > 1 { | |
name = id[1] | |
} | |
versionId := regexp.MustCompile(`VERSION_ID="?([.0-9]+)"?\n`).FindStringSubmatch(string(content)) | |
if len(versionId) > 1 { | |
version = versionId[1] | |
} | |
} | |
} | |
return | |
} | |
// GetDarwinVersion 获取darwin版本号 | |
// GetDarwinVersion Obtain the version number of Darwin | |
func GetDarwinVersion() (name, version string) { | |
getVer := func(p string) (o string) { | |
cmd := exec.Command("sw_vers", p) | |
stdout, _ := cmd.StdoutPipe() | |
cmd.Start() | |
content, err := ioutil.ReadAll(stdout) | |
if err == nil { | |
o = string(bytes.TrimSpace(content)) | |
} | |
return | |
} | |
name = getVer("-productName") | |
version = getVer("-productVersion") | |
return | |
} | |
// PathExists 检查路径存在 | |
// PathExists Check if the path exists | |
func PathExists(path string) (bool, error) { | |
_, err := os.Stat(path) | |
if err == nil { | |
return true, nil | |
} | |
if os.IsNotExist(err) { | |
return false, nil | |
} | |
return false, err | |
} | |
func main() { | |
os := NewOS() | |
log.Println(os) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
发现 macOS 的一些版本号获取方法