Home > Article > Backend Development > How do I Get the File Length in Go?
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."
The above is the detailed content of How do I Get the File Length in Go?. For more information, please follow other related articles on the PHP Chinese website!