Querying Drive Total Size in Windows using Go
Obtaining the total size of a drive is crucial for various applications. This article demonstrates how to retrieve this information using the standard Windows API call in Go.
As previously mentioned, the GetDiskFreeSpaceExW() function from kernel32.dll can be employed. It takes a directory path as input and provides three output parameters:
To utilize this function, allocate variables for the desired output:
import "syscall" kernelDLL := syscall.MustLoadDLL("kernel32.dll") GetDiskFreeSpaceExW := kernelDLL.MustFindProc("GetDiskFreeSpaceExW") var free, total, avail int64 path := "c:\" r1, r2, lastErr := GetDiskFreeSpaceExW.Call( uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))), uintptr(unsafe.Pointer(&free)), uintptr(unsafe.Pointer(&total)), uintptr(unsafe.Pointer(&avail)), )
Example output:
Free: 16795295744 Total: 145545281536 Available: 16795295744
This technique enables you to accurately determine the total size of any specified drive using the Windows API.
The above is the detailed content of How to Get a Windows Drive\'s Total Size Using Go?. For more information, please follow other related articles on the PHP Chinese website!