Skip to content

Instantly share code, notes, and snippets.

@zhenzou
Last active January 22, 2022 10:45
Show Gist options
  • Select an option

  • Save zhenzou/22153381867912ef21ab34baaec03a18 to your computer and use it in GitHub Desktop.

Select an option

Save zhenzou/22153381867912ef21ab34baaec03a18 to your computer and use it in GitHub Desktop.
handle checked error with go generic
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