Get file information in Golang
Example
package main
import (
"fmt"
"log"
"os"
)
func main() {
fileStat, err := os.Stat("test.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println("File Name:", fileStat.Name()) // Base name of the file
fmt.Println("Size:", fileStat.Size()) // Length in bytes for regular files
fmt.Println("Permissions:", fileStat.Mode()) // File mode bits
fmt.Println("Last Modified:", fileStat.ModTime()) // Last modification time
fmt.Println("Is Directory: ", fileStat.IsDir()) // Abbreviation for Mode().IsDir()
}
Output
File Name: test.txt
Size: 100
Permissions: -rw-rw-rw-
Last Modified: 2018-08-11 20:19:14.2671925 +0530 IST
Is Directory: false
Most Helpful This Week
Go program to read a text file character by character
Go program to add or append content at the end of text file
Move a file from one location to another in Golang
Golang Changing permissions, ownership, and timestamps
Reduce file size
Go program to extracting or unzip a Zip format file
Go program to read list of files inside Zip file
Golang program to delete a specific file
Golang Create Copy of a file at another location
Go program to compress list of files into Zip file