Standard library / errors
errors.As()
Finds the first matching error in the unwrap tree and assigns it through target; supports custom As methods. A non-nil target pointer to an error-implementing type or interface is required, otherwise it panics for non-nil err. Expected stdout: 'true x\n'.
Formal Syntax & Structure
go syntaxgo
func As(err error, target any) boolParameters & Attributes
| Name | Type | Description |
|---|---|---|
| err | error | Error to inspect or unwrap. |
| target | any | Error to match (Is) or non-nil destination pointer (As). |
Return Value
bool
Whether a matching error was assigned.
Working Example
Example Snippetgo
package main
import ( "errors" "fmt" "strconv")
func main() { _, e := strconv.Atoi("x") var n *strconv.NumError ok := errors.As(e, &n) fmt.Println(ok, n.Num)}