Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file length in Go?

Tags:

file

go

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?

like image 296
hardPass Avatar asked Jun 16 '13 12:06

hardPass


People also ask

How do I check the size of a file in go?

Go file size First, we get the FileInfo structure with os. Stat . Then we get the size of the file in bytes from the structure with Size function.

How do I find the content length of a file?

Using File#length() method A simple solution is to call the File#length() method that returns the size of the file in bytes. To get the file size in MB, you can divide the length (in bytes) by 1024 * 1024 .

How do I get the length of a string file?

You can check the length of a string using MyFile. Length , where MyFile is the string whose length you want to check. Create an If activity to check the length, and if the length is greater than 20 characters, use a Move File activity to move the file.

What is FileInfo length?

FileInfo has a Length property. It returns the size of a file in bytes. We use FileInfo and the Length property to measure file sizes.


1 Answers

(*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()) 
like image 71
Dominik Honnef Avatar answered Sep 29 '22 17:09

Dominik Honnef