2
我可以在scanner.go
看到该结构有一个error
方法。为什么go编译器说结构在接口不满足时不能满足接口?
// A SyntaxError is a description of a JSON syntax error.
type SyntaxError struct {
msg string // description of error
Offset int64 // error occurred after reading Offset bytes
}
func (e *SyntaxError) Error() string { return e.msg }
但是,编译器告诉我:
api/errors.go:24: impossible type switch case: err (type error) cannot have dynamic type json.SyntaxError (missing Error method)
试图做的类型
func myFunction(err error) {
switch err.(type) {
case validator.ErrorMap, json.SyntaxError:
response.WriteErrorString(http.StatusBadRequest, "400: Bad Request")
//etc
开关的情况下为什么这个不能编译时?因为该结构具有Error
方法。