Go Get the file size
Go, also known as Golang, is a programming language created by Google in 2007. It is a statically-typed language with syntax similar to C
but with memory safety, garbage collection, and structural typing.
Go is designed to be a language that is easy to read and write and well-suited for building large, concurrent and distributed systems.
One of the everyday tasks when working with files is to get their size, Go provides a simple way to get the size of a file using the os
package. In addition, go provides the os.Stat()
function, which can be used to get information about a file, including its size.
In this guide, we will look at how to use the os.Stat()
function to get the file size in bytes and how to convert it to other units of measurement such as kilobytes, megabytes, gigabytes, and terabytes.
Get File Size in Bytes using os.Stat()
function in Golang
The following code will open the file crawlingLog.txt
and print out its size in bytes.
The os.Open()
function is used to open the file, and the file.Stat()
function is used to get information about the file. Finally, the fileInfo.Size()
returns the file size in bytes.
package main
import (
"fmt"
"os"
)
func main() {
file, err := os.Open("C:\\Personal\\Code\\crawlingLog.txt")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("File size:", fileInfo.Size(), "bytes")
}
Output
File size: 306835 bytes
Note: This will not work if the file is opened in append mode.
If you want to get a file size without opening it, you can use the os.Stat()
function directly.
The os.Stat()
function takes the path
of the file as an argument and returns a os.FileInfo
struct, which contains information about the file, including its size.
You can use this method to check the file size of a file before reading it or in case you do not need to read the contents of the file.
Here's an example:
package main
import (
"fmt"
"os"
)
func main() {
fileInfo, err := os.Stat("C:\\Personal\\Code\\crawlingLog.txt")
if err != nil {
fmt.Println(err)
return
}
fmt.Println("File size:", fileInfo.Size(), "bytes")
}
Output
File size: 306835 bytes
Measure File Size in different units using Golang
You can use the os.Stat()
function to get the size of a file in bytes and then use that value to convert it to other units of measurement such as kilobytes, megabytes, gigabytes, and terabytes. Here's an example:
package main
import (
"fmt"
"os"
)
const (
byte = 1
kiloByte = 1024 * byte
megaByte = 1024 * kiloByte
gigaByte = 1024 * megaByte
teraByte = 1024 * gigaByte
)
func main() {
fileInfo, err := os.Stat("C:\\WINDOWS\\MEMORY.DMP")
if err != nil {
fmt.Println(err)
return
}
size := fileInfo.Size()
fmt.Printf("File size: %d bytes\n", size)
fmt.Printf("File size: %.2f KB\n", float64(size)/kiloByte)
fmt.Printf("File size: %.2f MB\n", float64(size)/megaByte)
fmt.Printf("File size: %.2f GB\n", float64(size)/gigaByte)
fmt.Printf("File size: %.2f TB\n", float64(size)/teraByte)
}
Output
File size: 2552715451 bytes
File size: 2492886.18 KB
File size: 2434.46 MB
File size: 2.38 GB
File size: 0.00 TB
Conclusion
In conclusion, Go provides a simple and efficient way to get the size of a file using the os.Stat()
function. By utilizing this function and the constants provided by Go, it is easy to retrieve the size of a file in bytes, kilobytes, megabytes, gigabytes, and terabytes.