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

How to Determine File Length in Go?

Linda Hamilton
Release: 2024-11-16 13:00:03
Original
993 people have browsed it

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

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!

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