GolangNote

Golang笔记

go 获取硬盘的可用空间的方法

Permalink

go 获取硬盘的可用空间,在POSIX 系统可以使用syscall.Statfs 库

Go: 获取硬盘可用空间
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))
}

输出:

Bash: 磁盘空间
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 转摘请注明来源

Related articles

Write a Comment to "go 获取硬盘的可用空间的方法"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms