How to Determine the Length of a File in Go?

Patricia Arquette
Release: 2024-11-13 08:42:02
Original
256 people have browsed it

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())
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template