Home  >  Article  >  Backend Development  >  How to Determine File Length in Go?

How to Determine File Length in Go?

Linda Hamilton
Linda HamiltonOriginal
2024-11-16 13:00:03918browse

How to Determine File Length in Go?

Determining 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn