Last active
January 22, 2022 10:45
-
-
Save zhenzou/22153381867912ef21ab34baaec03a18 to your computer and use it in GitHub Desktop.
handle checked error with go generic
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 | |
| func Check[T any](r T, err error) T { | |
| if err != nil { | |
| panic(err) | |
| } | |
| return r | |
| } | |
| func Check2[T1 any, T2 any](r1 T1, r2 T2, err error) (T1, T2) { | |
| if err != nil { | |
| panic(err) | |
| } | |
| return r1, r2 | |
| } | |
| func process() (int, error) { | |
| return 1, nil | |
| } | |
| func process2() (string, error) { | |
| return "test", nil | |
| } | |
| func main() { | |
| i := Check(process()) | |
| println(i) | |
| s := Check(process2()) | |
| println(s) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment