基本上,我希望输出为df -h,其中既包括可用空间,也包括卷的总大小.该解决方案需要在Windows、Linux和Mac上运行,并使用Go编写.

我已经翻阅了ossyscall围棋的文档,但没有找到任何东西.在Windows上,即使是命令行实用程序也不是很笨拙(dir C:\),就是需要提升权限(fsutil volume diskfree C:\).当然有一种方法可以做到这一点,但我还没有找到……

更新:

推荐答案

On POSIX systems you can use sys.unix.Statfs.
Example of printing free space in bytes of current working directory:

import "golang.org/x/sys/unix"
import "os"

var stat unix.Statfs_t

wd, err := os.Getwd()

unix.Statfs(wd, &stat)

// Available blocks * size per block = available space in bytes
fmt.Println(stat.Bavail * uint64(stat.Bsize))

对于Windows,您也需要使用syscall路由.示例(source,更新为匹配new sys/windows package):

import "golang.org/x/sys/windows"

h := windows.MustLoadDLL("kernel32.dll")
c := h.MustFindProc("GetDiskFreeSpaceExW")

var freeBytes int64

_, _, err := c.Call(uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(wd))),
    uintptr(unsafe.Pointer(&freeBytes)), nil, nil)

可以编写一个跨平台提供功能的包.

Go相关问答推荐

T的Golang通用切片,其中 *T实现接口

如何修复在Go API中使用Gin Framework的请求资源上没有使用Gin Framework的请求源的消息?''

编辑时保留YAML文件中的单引号

在Uber FX中实现后台进程正常关闭的正确方式是什么?

埃拉托塞尼筛:加快交叉关闭倍数步骤

重新赋值变量时未清除动态类型-这是错误吗?

可以';t从主机连接到ScyllaDB容器

go grpc:无法导入github.com/golang/protobuf/proto(没有所需的模块提供包github.com/gorang/protobuf-proto)

从文件读取字节,将其保存到 struct 体并修改值

如何根据地址和大小打印字符串

在 Go 中解组编号的 XML 标签

如何使用 go-git 将特定分支推送到远程

从 Makefile 运行时权限被拒绝

将shell输出绑定到Go中的 struct 的最佳方法?

未定义 protoc protoc-gen-go 时间戳

panic :拨号 tcp:在 172.22.64.1:53 上查找 bookstoreDB:没有这样的主机

Golang ACMEv2 HTTP-01 挑战不挑战服务器

函数调用中的类型参数panic

函数的递归调用以 goroutine 和惯用方式开始,以在所有工作 goroutine 完成时继续调用者

关于GO的几个问题