Standard library / os

os.PathError

Alias for io/fs.PathError containing operation, path and underlying error. Use errors.As to inspect and errors.Is to classify. Example uses a disposable temporary workspace. Expected stdout: 'true open missing\n'.

Formal Syntax & Structure

go syntaxgo
type PathError = fs.PathError

Working Example

Example Snippetgo
package main
import (  "errors"  "fmt"  "os")
func check(e error) {  if e != nil {    panic(e)  }}func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  _, e := os.Open("missing")  var p *os.PathError  fmt.Println(errors.As(e, &p), p.Op, p.Path)}
Go Reference