Welcome dear reader, today you will learn how to check if a file exists using the Go programming language. Filesystem checks appear in almost every application — reading configuration files, verifying uploads, or deciding whether to create a file first. Go makes this surprisingly easy, but there is one gotcha about error handling that beginners should know about.
Table of contents
- What is the os.Stat Function and How Does It Work?
- How to Check if a File Exists in Go
- What About Directories and Other Errors?
- Conclusion
What is the os.Stat Function and How Does It Work?
Go ships with the os package in its standard library, and the function you need is os.Stat(). It takes a path and returns two values: a file information object and an error. The trick to understanding Go here is that it does not return a simple true/false — instead, it tells you why a lookup failed through the error value. Note that this is different from the process in PHP that we covered here.
If the error equals os.ErrNotExist, the path does not exist. Any other error means something else went wrong, such as a permission problem. This explicit style is typical of Go: you handle each case yourself instead of relying on hidden behavior.
How to Check if a File Exists in Go
Here is a complete, runnable program that checks whether a file exists:
package main
import (
"errors"
"fmt"
"os"
)
func main() {
// The path we want to check
path := "notes.txt"
// os.Stat returns file info and an error
_, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
fmt.Println("File does not exist")
} else if err != nil {
fmt.Println("Other error:", err)
} else {
fmt.Println("File exists")
}
}
Run the program with go run main.go. We use errors.Is(err, os.ErrNotExist) rather than comparing the error with ==, because on some operating systems the underlying error is wrapped — errors.Is() checks the whole chain and is the recommended approach since Go 1.13.
You can also wrap this logic in a small helper function, which keeps your calling code readable:
// Exists reports whether the file at path exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
The function returns false, nil for a definitely missing file and an error only when something unexpected happened — a distinction worth keeping in your own code, too.
What About Directories and Other Errors?
os.Stat() works for both files and directories, so the same check answers “does this folder exist?”. If you need to distinguish them, inspect the returned info:
info, err := os.Stat(path) // Check whether the path is a directory isDir := info.IsDir()
Keep in mind that the file could disappear between your check and the moment you actually open it — a problem known as a race condition. When possible, simply call os.Open() and handle the ErrNotExist error instead of checking first.
Conclusion
That’s already it to check if a file exists with Golang. Use os.Stat(), match errors with errors.Is(err, os.ErrNotExist), and remember that the same call handles directories as well. Leave a comment below if you have any questions or feedback about the content of this article.
At re:Software S.L., we create innovative solutions for businesses that aim to digitize their processes and routines, mastering Golang helps us in this endeavour to build software that is stable and robust.

