os.ErrPermission
GoERRORCommonFilesystem

permission denied

Quick Answer

Use errors.Is(err, os.ErrPermission) to detect permission failures and check file mode or process UID.

What this means

Returned when the process lacks the required permissions to access a file or directory. Maps to EACCES on Linux.

Why it happens
  1. 1File permissions do not allow the current user to read/write
  2. 2Process running without required privileges

Fix

Check with errors.Is

Check with errors.Is
if err != nil && errors.Is(err, os.ErrPermission) {
    log.Fatal("permission denied, check file mode")
}

Why this works

errors.Is unwraps wrapped errors to match the os.ErrPermission sentinel.

Code examples
Detectgo
_, err := os.Open("/etc/shadow")
if errors.Is(err, os.ErrPermission) {
    fmt.Println("no access")
}
Chmod before opengo
os.Chmod("file.txt", 0644)
f, err := os.Open("file.txt")
Wrap and returngo
if err != nil {
    return fmt.Errorf("open cfg: %w", err)
}
Sources
Official documentation ↗

Go standard library

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Go errors