使用github.com/shirou/gopsutil库来获取机器信息,您可以按照以下步骤进行:

1、安装相关

go get github.com/shirou/gopsutil
go get github.com/yusufpapurcu/wmi
go get github.com/shirou/gopsutil/v3/disk

2、代码实现

package main

import (
	"fmt"

	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/host"
	"github.com/shirou/gopsutil/mem"
	"github.com/shirou/gopsutil/v3/disk"
)

func main() {

	// 获取主机信息
	hostInfo, err := host.Info()
	if err != nil {
		fmt.Printf("Failed to get host info: %v", err)
		return
	}
	fmt.Printf("Host info: %+vn", hostInfo)

	// 获取CPU信息
	cpuInfo, err := cpu.Info()
	if err != nil {
		fmt.Printf("Failed to get CPU info: %v", err)
		return
	}
	fmt.Printf("CPU info: %+vn", cpuInfo)

	// 获取内存信息
	memInfo, err := mem.VirtualMemory()
	if err != nil {
		fmt.Printf("Failed to get memory info: %v", err)
		return
	}
	fmt.Printf("Memory info: %+vn", memInfo)

	// 获取磁盘分区信息
	diskPartitions, err := disk.Partitions(true)
	if err != nil {
		fmt.Printf("Failed to get disk partitions: %v", err)
		return
	}
	fmt.Printf("Disk partitions: %+vn", diskPartitions)
	for _, partition := range diskPartitions {
		// 获取每个磁盘分区使用情况
		usage, err := disk.Usage(partition.Mountpoint)
		if err != nil {
			fmt.Printf("Failed to get disk usage for %s: %v", partition.Mountpoint, err)
			continue
		}
		fmt.Printf("%s usage: %+vn", partition.Mountpoint, usage)
	}
}

原文地址:https://blog.csdn.net/qq_21275565/article/details/131297086

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_48498.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注