Golang os.Lstat(), Stat() functions and FileMode type example
package os
Golang os.Lstat(), Stat() functions and FileMode type usage example
package main
import (
"fmt"
"os"
)
func main() {
// can handle symbolic link, but will no follow the link
fileInfo, err := os.Lstat("file.txt")
// cannot handle symbolic link
//fileInfo, err := os.Lstat("file.txt")
if err != nil {
panic(err)
}
fmt.Println("Name : ", fileInfo.Name())
fmt.Println("Size : ", fileInfo.Size())
fmt.Println("Mode/permission : ", fileInfo.Mode())
// --- check if file is a symlink
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
fmt.Println("File is a symbolic link")
}
fmt.Println("Modification Time : ", fileInfo.ModTime())
fmt.Println("Is a directory? : ", fileInfo.IsDir())
fmt.Println("Is a regular file? : ", fileInfo.Mode().IsRegular())
fmt.Println("Unix permission bits? : ", fileInfo.Mode().Perm())
fmt.Println("Permission in string : ", fileInfo.Mode().String())
fmt.Println("What else underneath? : ", fileInfo.Sys())
}
Sample output :
Name : file.txt
Size : 95
Mode/permission : -rw-r--r--
Modification Time : 2015-06-19 12:00:55 +0800 SGT
Is a directory? : false
Is a regular file? : true
Unix permission bits? : -rw-r--r--
Permission in string : -rw-r--r--
What else underneath? : &{16777218 33188 1 6421054 501 20 0 [0 0 0 0] {1434686458 0} {1434686455 0} {1434686455 0} {1434685089 0} 95 8 4096 0 0 0 [0 0]}
References :
http://golang.org/pkg/os/#FileMode
Advertisement
Something interesting
Tutorials
+27.2k Golang : Find files by name - cross platform example
+19.3k Golang : Get RGBA values of each image pixel
+11k Golang : Create S3 bucket with official aws-sdk-go package
+15.2k Golang : How to add color to string?
+6.8k Android Studio : Hello World example
+7.9k Golang Hello World Example
+10.6k Golang : Flip coin example
+19.6k Golang : Get current URL example
+14.4k Golang : Parsing or breaking down URL
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+18.9k Golang : Read input from console line
+17.6k Golang : Parse date string and convert to dd-mm-yyyy format