find the size of a file in go

To find the size of a file in Go, you can use the os package and its Stat function. The Stat function returns a FileInfo object that contains information about the specified file, including its size. Here's an example:

main.go
package main

import (
    "fmt"
    "os"
)

func main() {
    file, err := os.Stat("example.txt")

    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    size := file.Size()
    fmt.Println("Size:", size)
}
234 chars
19 lines

In this example, we use the os.Stat function to get information about the file "example.txt". If an error occurs, we print the error and exit the function. Otherwise, we get the file's size using the Size method of the returned FileInfo object, and print it to the console.

related categories

gistlibby LogSnag