Skip to content

Instantly share code, notes, and snippets.

@christian-korneck
Created February 5, 2022 13:30
Show Gist options
  • Save christian-korneck/eb1bcb226d8317db2464f12b6567f1bf to your computer and use it in GitHub Desktop.
Save christian-korneck/eb1bcb226d8317db2464f12b6567f1bf to your computer and use it in GitHub Desktop.
golang errors.As()
package main
import (
"errors"
"fmt"
"io/fs"
"os"
)
// output: Failed at path: non-existing
func main() {
if _, err := os.Open("non-existing"); err != nil {
var pathError *fs.PathError
if errors.As(err, &pathError) {
fmt.Println("Failed at path:", pathError.Path)
} else {
fmt.Println(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment