cast
cast copied to clipboard
Wrap a common "cast error" for To___E functions
To make it easier to use Go1.13's errors.Is
and errors.As
, it'd be great if the To___E
functions wrap a common "cast error" so that transitive callers to these functions can determine if an error comes from casting.
A slightly-silly example usecase:
func main() {
val, err := maybeCast("not_an_int")
if errors.Is(err, cast.Err) {
fmt.Println("unable to cast!")
}
fmt.Println(val)
}
func maybeCast(myVal string) (int, error) {
if someCondition() {
return cast.ToIntE(myVal)
}
return fmt.Errorf("unrelated error")
}
If there's interest, I'd love to make a PR. :)