// when passing in a schema.MultiError
func FormatErrForResp(err error) error {
fmt.Println(reflect.TypeOf(err)) // prints schema.MultiError
what, ok := err.(schema.MultiError)
fmt.Println(what) // prints "0 errors" (zero value for MultiError)
fmt.Println(ok) // prints false
switch v := err.(type) {
case schema.MultiError:
// do something
default:
// do something
}
}
Reflection shows that it's of typeschema.MultiError
but both the type switch and the type assertion are failing. It falls to the default case
评论:
iygwaz:
titpetric:Additional information: in the function where the error is first encountered, the type switch goes through the correct case:
if err := schema.NewDecoder().Decode(dst, r.Form); err != nil { switch err.(type) { case schema.MultiError: // passes through here (correct) default: // } return err }
Please put a working sample on the go playground, it will make it easier for us to inspect what you have outside of this function.
Are you sure you're not using a pointer, and the case should be
case *schema.MultiError
?Edit: here's my version of a working playground, so I'm betting there's some pointer mixup from what I've seen dealing with type switches.
