Home > Backend Development > Golang > How to Get a Windows Drive\'s Total Size Using Go?

How to Get a Windows Drive\'s Total Size Using Go?

Mary-Kate Olsen
Release: 2024-11-27 16:02:10
Original
858 people have browsed it

How to Get a Windows Drive's Total Size Using Go?

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:

  • lpFreeBytesAvailableToCaller: Free bytes accessible to the caller
  • lpTotalNumberOfBytes: Total disk size
  • lpTotalNumberOfFreeBytes: Unused space on the disk

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)),
)
Copy after login

Example output:

Free: 16795295744 Total: 145545281536 Available: 16795295744
Copy after login

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!

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