go 获取硬盘的可用空间,在POSIX 系统可以使用syscall.Statfs 库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
var stat syscall.Statfs_t
wd, _ := os.Getwd()
syscall.Statfs(wd, &stat)
// Available blocks * size per block = available space in bytes
fmt.Println(stat.Bavail * uint64(stat.Bsize))
}
输出:
1
2
3
4
5
131730866176
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/disk0s2 176757808 47856760 128645048 28% /
/dev/disk0s4 67697660 24802928 42894732 37% /Volumes/BOOTCAMP
本文网址: https://golangnote.com/topic/108.html 转摘请注明来源