Home > Article > Backend Development > How can I retrieve a list of all drives on a Windows system using Go?
Retrieving a List of All Drives on Windows Using Go
Listing drives is essential when accessing data from multiple storage devices. In Windows, the GetLogicalDrives function provides a way to obtain a bit map representing the available drives.
To convert the bit map to a list of drive letters, follow these steps:
<code class="go">import "syscall" driveBitmap := uint32(syscall.GetLogicalDrives())</code>
<code class="go">func bitsToDrives(bitMap uint32) []string { var drives []string availableDrives := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} for i := range availableDrives { if bitMap&1 == 1 { drives = append(drives, availableDrives[i]) } bitMap >>= 1 } return drives }</code>
<code class="go">drives := bitsToDrives(driveBitmap) for _, drive := range drives { // Perform operation on drive }</code>
By using this approach, your program can automatically iterate through all drives on a Windows system without requiring the user to specify drive letters manually.
The above is the detailed content of How can I retrieve a list of all drives on a Windows system using Go?. For more information, please follow other related articles on the PHP Chinese website!