0

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))
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
Zander Wong
  • 639
  • 2
  • 8
  • 15
  • I assume you want to know this for Windows only and that the usage of a package is not a problem? – Dippo Apr 25 '18 at 16:31
  • 1
    in windows we can use [`IOCTL_STORAGE_GET_DEVICE_NUMBER`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb968800(v=vs.85).aspx) for get device(disk) number. how/are possible use this in go separate question – RbMm Apr 25 '18 at 16:31
  • @Dippo yep. I tried to find some pkg of golang, but doesn't work. – Zander Wong Apr 25 '18 at 17:35
  • @Zander Wong I also didn't find a package but i did found out that you can enter this one in the command line, this works for win 7, 8 and 10: wmic logicaldisk get name – Dippo Apr 25 '18 at 18:45
  • 1
    @Zander Wong I think i found a package : https://github.com/StackExchange/wmi – Dippo Apr 25 '18 at 18:50
  • @Dippo this command just list partitions of my hard-disk not the disk numbers. – Zander Wong Apr 26 '18 at 06:00
  • @Dippo Thx Very Much! I just tested the pkg `wmi`. It works! Thx again. – Zander Wong Apr 26 '18 at 06:33
  • 1
    @Zander Wong You are right that partitios are still visible as a drive, i was wrong because i don't have any partitions. But i found on this website (https://ardamis.com/2012/08/21/getting-a-list-of-logical-and-physical-drives-from-the-command-line/) a query that solves your problem. – Dippo Apr 26 '18 at 18:03
  • @Dippo I fount a command line to get hard-disk index. `$ wmic diskdrive get index` – Zander Wong Apr 27 '18 at 15:05

0 Answers0