假设我在目录上执行 os.Stat()
:
func main() {
fi, _ := os.Stat("/tmp")
println(fi.Size())
}
// 548
// Program exited.
https://play.golang.org/p/NIzGMHRYfi
[FileInfo].Size()
值究竟代表什么?这不是文件大小,所以我猜是文件数量之类的东西? inode ?我在任何地方都找不到明确的答案,所以也许有人可以启发我?
最佳答案
FileInfo 提及
// length in bytes for regular files; system-dependent for others
所以这真的取决于执行环境。
例如参见“Where does ext4 store directory sizes?”
在该示例中,目录统计大小返回 4096 字节。
这是目录本身的实际大小,而不是它包含的内容。
The stat command provides no facility for querying anything other then the size of a filesystem object (directory or file).
stat
simply doesn't have a way to return multiple sizes—so it can only return the size of the directory itself, not of its contents.
And also "directory size including contents" becomes less clear when you have hardlinked files.
关于go - 来自 os.Stat/Lstat 的目录大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593335/