This is a creation in Article, where the information may have evolved or changed.
System inspection Applications (1)
Test environment:
Content
Getenvfunc (): Get environment variable function
Getenvsvr (): Currently only obtains CPU cores
Code
Package Mainimport ("bytes" "detect_sys/ming" "FMT" "OS" "os/exec"/* "StrConv" */"s Trings ")//OSENV system environment variable gets the type osenv struct {envuser string Envhome string Pid int}func main () {FMT. Println ("Start") fmt. Println (Getenvfunc ()) Getenvsvr () Ming. Strfunc () fmt. Println ("End")}//getenvfunc gets the environment variable function func getenvfunc () (res osenv) {Res. Envuser = OS. Getenv ("USER") Res. envhome = OS. Getenv ("HOME") Res. Pid = OS. Getpid () return res}//Getenvsvr 1. Gets the number of CPU cores func getenvsvr () int {cmd: = exec. Command ("/bin/bash", "-C", "Lscpu |grep ' CPU (s) ' |grep-v ' li ' |grep-v '-' |awk ' {print '} '") var out bytes. Buffer cmd. Stdout = &out Err: = cmd. Run () if err! = Nil {fmt. PRINTLN ("Error") return-1}//Remove space str: = strings. Replace (out. String (), "", "",-1)//remove line break str = strings. Replace (str, "\ n", "",-1) fmt. PRINTLN ("CPU Cores:" + str) return 1}