How to get file information in Go

In the Go language, working with files is a common task, and often there's a need to get detailed information about a file. This can include accessing data such as file size, permissions, creation or modification timestamp, and more. In this article, we'll see how to gather this information easily using Go's os package.

To get information about files, the first step is to import the os package. This package provides functions and types for working with the file system and offers several features for manipulating files and directories.


package main

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

The main function for obtaining information about a file is os.Stat. This function takes the file path as input and returns an os.FileInfo object containing the requested information.


func main() {
    fileInfo, err := os.Stat("path/to/your/file.txt")
    if err != nil {
        log.Fatal(err)
    }

    // Use the obtained information
    fmt.Println("File Name:", fileInfo.Name())
    fmt.Println("File Size:", fileInfo.Size())
    fmt.Println("Permissions:", fileInfo.Mode())
    fmt.Println("Modification Time:", fileInfo.ModTime())
    fmt.Println("Is Directory:", fileInfo.IsDir())
}

In the code above:

  • fileInfo.Name() returns the file name.
  • fileInfo.Size() returns the file size in bytes.
  • fileInfo.Mode() returns the file permissions as an os.FileMode value.
  • fileInfo.ModTime() returns the file's last modification timestamp.
  • fileInfo.IsDir() returns a boolean value indicating whether the specified path is a directory.

It's important to handle any errors when using os.Stat, as the file might not exist or there might be an access issue. For example, os.Stat will return an error if the specified file does not exist.

In addition to os.Stat, Go also provides other useful functions for obtaining information and manipulating files, such as os.Lstat, os.Readlink, and os.Open.

If you want to get information about a file that might be a symbolic link, you can use os.Lstat instead of os.Stat. The main difference is that os.Stat follows symbolic links to the target file, while os.Lstat returns information about the symbolic link itself.


fileInfo, err := os.Lstat("path/to/symlink")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Is Symbolic Link:", (fileInfo.Mode() & os.ModeSymlink != 0))

With Go, you can also examine file permissions using the os.FileMode type. This type allows you to check if a file is readable, writable, or executable.


if fileInfo.Mode().Perm()&(1<<(uint(7))) != 0 {
    fmt.Println("File is readable")
}
if fileInfo.Mode().Perm()&(1<<(uint(7)-1)) != 0 {
    fmt.Println("File is writable")
}
if fileInfo.Mode().Perm()&(1<<(uint(7)-2)) != 0 {
    fmt.Println("File is executable")
}

Conclusion

Go makes it very simple to obtain detailed information about a file through the os.Stat function and the os.FileInfo type. With the returned information, you can check essential file properties, such as name, size, permissions, and modification timestamp. Knowing how to use these features is essential for efficiently managing files in Go, whether you're developing a simple application or a complex file management system.

Back to top