根据man page for FileInfo ,以下信息可用时 stat()
在 Go 中读取一个文件:
type FileInfo interface {
Name() string // base name of the file
Size() int64 // length in bytes for regular files; system-dependent for others
Mode() FileMode // file mode bits
ModTime() time.Time // modification time
IsDir() bool // abbreviation for Mode().IsDir()
Sys() interface{} // underlying data source (can return nil)
}
如何在 Go 中检索到特定文件的硬链接(hard link)数?
UNIX ( <sys/stat.h>
) 定义 st_nlink
(“硬链接(hard link)的引用计数”)作为 stat()
的返回值系统调用。
最佳答案
例如,在 Linux 上,
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
fi, err := os.Stat("filename")
if err != nil {
fmt.Println(err)
return
}
nlink := uint64(0)
if sys := fi.Sys(); sys != nil {
if stat, ok := sys.(*syscall.Stat_t); ok {
nlink = uint64(stat.Nlink)
}
}
fmt.Println(nlink)
}
输出:
1
关于unix - 计算 Go 中文件的硬链接(hard link),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26854961/