As in Go

Posted by GoDoc
Public (Editable by Users)

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

Go
Edit
package main

import (
	"errors"
	"fmt"
	"os"
)

func main() {
	if _, err := os.Open("non-existing"); err != nil {
		var pathError *os.PathError
		if errors.As(err, &pathError) {
			fmt.Println("Failed at path:", pathError.Path)
		} else {
			fmt.Println(err)
		}
	}
}