Skip to content

Instantly share code, notes, and snippets.

@DeadlySurgeon
Created February 5, 2025 13:19
Show Gist options
  • Save DeadlySurgeon/d220ac163062f145b0044ad9337baf18 to your computer and use it in GitHub Desktop.
Save DeadlySurgeon/d220ac163062f145b0044ad9337baf18 to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
)
func main() {
err := wrong()
fmt.Println(As[*Aerror](err))
}
func wrong() error { return &Aerror{} }
func As[T error](err error) (t T) {
target := new(T)
if errors.As(err, target) {
return *target
}
return
}
type Aerror struct{}
func (a *Aerror) Error() string { return "duck" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment