在类型比较时编译错误

时间:2016-08-17 18:47:43

标签: google-app-engine go

我得到下面显示的编译错误,说ErrFieldMismatch类型缺少Error()方法,但是如上一个代码块所示,它不是。

为什么我不能为此进行类型比较?

错误

impossible type switch case: err (type error) cannot have dynamic type "google.golang.org/appengine/datastore".ErrFieldMismatch (missing Error method)

我的代码

type Program struct {
    aemodel.Base

    Name        string   `json:"name" required:"true"`
    Public      bool     `json:"isPublic"`
    Description string   `json:"description" required:"true"`
    Default     bool     `json:"isDefault"`
    Tags        []string `json:"tags"`

    // Level int
}

// Load - PropertyLoadSaver interface
func (p *Program) Load(ps []datastore.Property) error {
    if err := datastore.LoadStruct(p, ps); err != nil {
        switch err.(type) {
        case datastore.ErrFieldMismatch:  // <-- Failure point
            return nil
        default:
            return err
        }
    }
    return nil
}

appengine代码

type ErrFieldMismatch struct {
    StructType reflect.Type
    FieldName  string
    Reason     string
}

func (e *ErrFieldMismatch) Error() string {
    return fmt.Sprintf("datastore: cannot load field %q into a %q: %s",
        e.FieldName, e.StructType, e.Reason)
}

1 个答案:

答案 0 :(得分:3)

$(".someClass").click(function(){ var id = $(this).attr("id"); var cls = this.className; alert(id); alert(cls); }); 方法定义在指向Error的指针的类型上,即datastore.ErrFieldMismatch上定义了Error,因此它只是{{} 1}}实现*datastore.ErrFieldMismatch接口。

尝试更改*datastore.ErrFieldMismatch表达式:

Error