Home > Article > Backend Development > How to Determine File Length in Go?
Question:
Despite exploring the golang.org/pkg/os/#File documentation, you're unable to locate a method for obtaining file length. Is this an oversight?
Answer:
The (*os.File).Stat() method can be used to retrieve a os.FileInfo value, which provides a Size() method. Here's how it's implemented:
fi, err := f.Stat() if err != nil { // Handle error } fmt.Printf("The file is %d bytes long", fi.Size())
This code attempts to retrieve the file's information using Stat(), and if successful, it prints the file length using the Size() property of the os.FileInfo value.
The above is the detailed content of How to Determine File Length in Go?. For more information, please follow other related articles on the PHP Chinese website!