HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgoCritical

How to get file length in Go?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
lengthhowfileget

Problem

I looked up golang.org/pkg/os/#File , but still have no idea.
Seems there is no way to get file length, did I miss something?

How to get file length in Go?

Solution

(*os.File).Stat() returns a os.FileInfo value, which in turn has a Size() method. So, given a file f, the code would be akin to

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

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

Code Snippets

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

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

Context

Stack Overflow Q#17133590, score: 181

Revisions (0)

No revisions yet.