Created
February 5, 2025 13:19
-
-
Save DeadlySurgeon/d220ac163062f145b0044ad9337baf18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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