In Go, the os.File type provides a straightforward method for retrieving the length of a file handled by the File pointer.
To determine the length of a file, you can leverage the Stat function provided by the os package:
fi, err := f.Stat()
fmt.Printf("The file is %d bytes long", fi.Size())
To illustrate the retrieval process, consider the following code snippet:
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()) }
By executing this code, you can retrieve and display the length of the specified file, "my_file.txt."
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3