Skip to content

Instantly share code, notes, and snippets.

@locaIhost
Forked from toddlers/fileinfo.go
Created August 27, 2025 19:31
Show Gist options
  • Save locaIhost/0cf38aa3653517de473e3858aa5f0d54 to your computer and use it in GitHub Desktop.
Save locaIhost/0cf38aa3653517de473e3858aa5f0d54 to your computer and use it in GitHub Desktop.
getting fileinfo in go
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())
}

Comments are disabled for this gist.