dongqi7631 2018-04-25 15:15
浏览 324

在golang中,如何获取Windows上的硬盘数量(不是分区!)?

This question is different from How can I get a listing of all drives on Windows using golang?

I know how get the partitions of hard disk, but what about the numbers of hard disk? I tried using diskpark but don't like it.
enter image description here

Is there some native go way to implement?

@Dippo saids: I think i found a package : https://github.com/StackExchange/wmi

And it works . Thx all.

Here the code is.

Gopkg.toml:

required = ["github.com/stackexchange/wmi"]

and the go code:

func getDiskDrivers() uint8 {
    type Win32_DiskDrive struct {
        Caption      string
        Name         string
        DeviceID     string
        Model        string
        Index        int
        Partitions   int
        Size         int
        PNPDeviceID  string
        Status       string
        SerialNumber string
        Manufacturer string
        MediaType    string
        Description  string
        SystemName   string
    }

    var dst []Win32_DiskDrive

    query := wmi.CreateQuery(&dst, "")
    if err := wmi.Query(query, &dst); err != nil {
        log.Println(err.Error())
        return 0
    }

    //for key, value := range dst {
    //  log.Println(fmt.Sprintf(`Disk%d: %v`, key+1, value))
    //}

    return uint8(len(dst))
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)