25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →
Getting File Size and Modification Time in Go
Code Snippet

Getting File Size and Modification Time in Go

go

The os.Stat function returns a FileInfo interface that provides comprehensive metadata about a file or directory. This includes the file size, modification time, permission bits, and whether the path is a directory. Accessing this information is efficient since os.Stat makes a single system call.

The Size() method returns the file size in bytes, while ModTime() returns a time.Time value representing the last modification timestamp. You can use IsDir() to determine if the path is a directory rather than a regular file. The Mode() method provides the file’s permission bits and other mode information.

This approach is ideal when you need to gather multiple pieces of information about a file at once. If you only need to check existence without examining details, or if you need information about symbolic links themselves, consider using os.Lstat instead.

Further Reading: