Golang System Info Guide
To obtain system information, you can utilize the runtime package and os package in the Go language.
Here are some ways to obtain system information:
- Obtain the name and architecture of the operating system:
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println("操作系统:", runtime.GOOS)
fmt.Println("架构:", runtime.GOARCH)
}
- Retrieve the number of CPU cores:
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println("CPU核心数:", runtime.NumCPU())
}
- Get memory information.
package main
import (
"fmt"
"runtime"
)
func main() {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
fmt.Println("总分配的内存:", memStats.TotalAlloc)
fmt.Println("堆内存使用量:", memStats.HeapAlloc)
fmt.Println("堆内存释放量:", memStats.HeapReleased)
fmt.Println("堆内存对象数:", memStats.HeapObjects)
}
- Accessing environmental variables:
package main
import (
"fmt"
"os"
)
func main() {
envVars := os.Environ()
for _, envVar := range envVars {
fmt.Println(envVar)
}
}
These methods can assist you in obtaining information about the operating system, hardware, and environment.