Golang os.Stat() Function: A Comprehensive Guide

Golang os package provides several functions and types for interacting with the operating system. It includes functions for working with files and directories, process management, and other standard system tasks.

Golang os.Stat()

Golang os.Stat() is a built-in function used to get the file status for a given file or directory. It returns a FileInfo type, which contains information about the file or directory, such as its size, modification time, and mode.

Syntax

func Stat(name string) (FileInfo, error)

Parameters

The Stat() function takes a file path as an argument.

Return value

The Stat() function returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

The FileInfo object contains

  1. Name() string: It is the base name of the file,
  2. Size() int64: It is the length in bytes for regular files; system-dependent for others,
  3. Mode() FileMode: It is the file mode bits.
  4. ModTime() time.Time: It is the modification time.
  5. IsDir() bool: It is an abbreviation for Mode().IsDir()
  6. Sys() any:

Example

package main

import (
  "fmt"
  "os"
)

func main() {
  path_to_file := "../file.txt"
  fileInfo, err := os.Stat(path_to_file)
  if err != nil {
    panic(err)
  }
  
  // Use the FileInfo type to access the file's properties
  fmt.Println("File Name:", fileInfo.Name())
  fmt.Println("File size:", fileInfo.Size())
  fmt.Println("Last modified:", fileInfo.ModTime())
  fmt.Println("Permissions:", fileInfo.Mode())
}

Output

File Name: file.txt
File size: 12
Last modified: 1979-11-30 00:00:00 +0530 IST
Permissions: -rw-rw-r--

In this example, the os.Stat() function is used to get the FileInfo type for the file.txt.

If the file exists, the FileInfo type is used to access various properties of the file, such as its name, size, last modified time, and permissions.

How to check if file exists in Golang

To check if a file exists in Golang, use the os.Stat() function. The os.Stat() function returns a FileInfo type, which contains information about the file, such as its size and modification time. If the file does not exist, os.Stat() will return an error.

package main

import (
  "fmt"
  "os"
)

func main() {
  path_to_file := "../file.txt"
  fileInfo, err := os.Stat(path_to_file)
  if err != nil {
    panic(err)
  }
  
  // Use the FileInfo type to access the file's size
  fmt.Println("File size:", fileInfo.Size())
}

Output

File size: 12

In this example, the os.Stat() function is used to get the FileInfo type for the file.txt. If the file exists, the FileInfo type is used to access the file’s size. If the file does not exist, the err variable will contain an error message.

Let’s see a scenario where the file does not exist and see the output.

func main() {
  path_to_file := "file.txt"
  fileInfo, err := os.Stat(path_to_file)
  if err != nil {
   panic(err)
  }
  // Use the FileInfo type to access the file's size
  fmt.Println("File size:", fileInfo.Size())
}

Output

panic: stat file.txt: no such file or directory

goroutine 1 [running]:
main.main()
 /Users/krunallathiya/Desktop/Code/R/gol/hello.go:12 +0xb0
exit status 2

And we get the error.

Important Note!

Go os.Stat() function only works on regular files and directories. If you attempt to call os.Stat() on a symbolic link will return the FileInfo type for the target of the symbolic link, not the symbolic link itself.

To check if a symbolic link exists, use the os.Lstat() function.

Conclusion

To get the information about files in Golang, use the os.Stat() function.

To check if a file exists in Go, use the os.Stat() function.

That’s it.

Leave a Comment