How to Calculate the Total Size of a Windows Drive Using Go?

Patricia Arquette
Release: 2024-11-21 08:02:10
Original
356 people have browsed it

How to Calculate the Total Size of a Windows Drive Using Go?

Calculating the Total Size of a Drive in Go on Windows

To obtain the total size of a specific drive in Go on Windows, the standard Windows API call to be used is GetDiskFreeSpaceExW() from kernel32.dll. This function retrieves the free space available to the caller, along with the total number of bytes on the disk and the total number of free bytes.

By providing pointers for each piece of information required, the GetDiskFreeSpaceExW() function can be utilized to retrieve the total size of a drive.

Here's an example code demonstrating its usage:

import (
    "syscall"
    "unsafe"
    "fmt"
)

func main() {
    kernelDLL, _ := syscall.LoadDLL("kernel32.dll")
    GetDiskFreeSpaceExW, _ := kernelDLL.FindProc("GetDiskFreeSpaceExW")

    var free int64
    var total int64
    var 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)),
    )

    fmt.Println(r1, r2, lastErr)
    fmt.Println("Free:", free, "Total:", total, "Available:", avail)
}
Copy after login

Running the above code will generate an output similar to:

1 0 Success.
Free: 16795295744 Total: 145545281536 Available: 16795295744
Copy after login

The above is the detailed content of How to Calculate the Total Size of a Windows Drive Using Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template