Golang ProgramsGolang Programs

Get file information in Golang

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()
}
C:\golang\working-with-files>go fmt example.go
example.go

C:\golang\working-with-files>golint example.go

C:\golang\working-with-files>go run example.go
File Name: test.txt
Size: 100
Permissions: -rw-rw-rw-
Last Modified: 2018-08-11 20:19:14.2671925 +0530 IST
Is Directory: false

C:\golang\working-with-files>