小编典典

如何在Go中获取文件长度?

go

如何在Go中获取文件长度?


阅读 252

收藏
2021-12-08

共2个答案

小编典典

(*os.File).Stat()返回一个os.FileInfo值,该值又具有一个Size()方法。所以,给定一个文件f,代码类似于

fi, err := f.Stat()
if err != nil {
  // Could not obtain stat, handle error
}

fmt.Printf("The file is %d bytes long", fi.Size())
2021-12-08
小编典典

如果不想打开文件,可以直接调用os.Stat

fi, err := os.Stat("/path/to/file")
if err != nil {
    return err
}
// get the size
size := fi.Size()
2021-12-08