Home >Backend Development >Golang >How to Determine the Length of a File in Go?

How to Determine the Length of a File in Go?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-13 08:42:02291browse

How to Determine the Length of a File in Go?

Determining File Length in Go

The question arises: how can you determine the length of a file in Go? While the docs for golang.org/pkg/os/#File may not explicitly mention it, there is a simple method to extract this information.

To obtain the file length in Go, follow these steps:

  1. Use the Stat() method on the *os.File object to retrieve the file's information into an os.FileInfo object.
  2. Access the Size() method on the os.FileInfo object to obtain the file's length.

Here's an example code snippet:

fi, err := f.Stat()
if err != nil {
  // Could not obtain stat, handle error
}

fmt.Printf("The file is %d bytes long", fi.Size())

This code snippet assumes that f is an *os.File object representing the file whose length you want to determine.

The above is the detailed content of How to Determine the Length of a File 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