Welcome To Golang By Example

Get File Name, Size, Permission Bits, Mode, Modified Time in Go (Golang)

Table of Contents

Overview

os.Stat() function can be used to the info of a file in go. This function returns stats which can be used to get

Below is the signature of the function. It takes in the named file and return FileInfo struct which defines utility method to get above information

func Stat(name string) (FileInfo, error)

Code

package main

import (
    "fmt"
    "log"
    "os"
)

func main() {
    //Create a file
    file, err := os.Create("temp.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    //Write something to the file
    file.WriteString("some sample text" + "\n")

    //Gets stats of the file
    stats, err := os.Stat("temp.txt")
    if err != nil {
        log.Fatal(err)
    }

    //Prints stats of the file
    fmt.Printf("Permission: %s\n", stats.Mode())
    fmt.Printf("Name: %s\n", stats.Name())
    fmt.Printf("Size: %d\n", stats.Size())
    fmt.Printf("Modification Time: %s\n", stats.ModTime())
}

Output:

Permission: -rwxrwxrwx
Name: temp.txt
Size: 17
Modification Time: 2020-04-16 22:26:47.080128602 +0530 IST