如何在Go中获取文件长度? [英] How to get file length in Go?

查看:164
本文介绍了如何在Go中获取文件长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查找了 golang.org/pkg/os/#File ,但还是不知道. 似乎无法获取文件长度,我错过了什么吗?

I looked up golang.org/pkg/os/#File , but still have no idea. Seems there is no way to get file length, did I miss something?

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

How to get file length in Go?

推荐答案

(*os.File).Stat()返回os.FileInfo值,而该值又具有Size()方法.因此,给定文件f,代码将类似于

(*os.File).Stat() returns a os.FileInfo value, which in turn has a Size() method. So, given a file f, the code would be akin to

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

fmt.Printf("The file is %d bytes long", fi.Size())

这篇关于如何在Go中获取文件长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆