As
Overview
As finds the first error in the chain assignable to target.
Part of the err package — Node.js analog: http-errors.
Signature
go
func As(err error, target any) boolCompare: Node.js · Standard Go · gox
js
// Typical http-errors pattern in Node.jsgo
errors.Is(err, target)
errors.As(err, &target)
fmt.Errorf("context: %w", err)go
import "github.com/sahilkhaire/gox/err"
var appErr err.AppError
if err.As(err, &appErr) { /* ... */ }Example
go
import "github.com/sahilkhaire/gox/err"
var appErr err.AppError
if err.As(err, &appErr) { /* ... */ }Tips
Import github.com/sahilkhaire/gox/err and call As directly. See the comparison below for the standard library equivalent.
Standard library alternative
Use the standard library directly:
go
errors.Is(err, target)
errors.As(err, &target)
fmt.Errorf("context: %w", err)