在 Go 中,os.File 类型提供了一种简单的方法来检索由 File 指针处理的文件的长度。
要确定文件的长度,您可以利用操作系统提供的 Stat 函数package:
fi, err := f.Stat()
fmt.Printf("The file is %d bytes long", fi.Size())
为了说明检索过程,请考虑以下代码片段:
package main import ( "fmt" "os" ) func main() { f, err := os.Open("my_file.txt") if err != nil { fmt.Println("Could not open file:", err) return } fi, err := f.Stat() if err != nil { fmt.Println("Could not obtain file info:", err) return } fmt.Printf("The file is %d bytes long", fi.Size()) }
通过执行此代码,您可以检索并显示指定文件“my_file.txt”的长度。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3