os.ErrNotExist
The sentinel error Go file functions return, wrapped, when a file or folder does not exist.
In more detail
Test for it with errors.Is(err, os.ErrNotExist), never by comparing message text, which differs between systems. Treat not-exist as an expected case, such as falling back to defaults, and any other error as a real problem.
Example
case errors.Is(err, os.ErrNotExist):
fmt.Printf("%s: absent\n", path)Related terms
- sentinel error — A specific error value published in a package-level variable, such as io.EOF or os.ErrNotExist, that callers can test for with errors.Is.
- errors.Is — The function that reports whether an error, or any error wrapped inside it, is a particular target error.
- stat — The operating system call, and matching command, that reports a file's details such as size, mode and modification time without opening it.
Taught in
- Files and the os Package — Concurrent Systems Programming with Go