我想这样做如下:创建一个接口,其他接口的片
type Model interface {
EntityType() string
GetKey() *datastore.Key
SetKey(*datastore.Key) error
PreSave(context.Context) error
PostSave(context.Context) error
PostLoad(context.Context) error
}
type Models []Model interface {
Prepare(int) ([]Model, error)
}
因此struct Models
也是一个接口,将由结构的片得到实现实施Model
。类似以下内容:
type Foo struct {
key *datastore.Key `datastore:"_"`
// ... other things here
}
// assume all Model interface funcs are here and valid
type Foos []Foo
func (f *Foos) Prepare (num int) ([]Model, error) {
// do the preparations for Foo slice
}
显然,上面的代码会引发错误并且不可能。但是有没有一些代码可以产生基本相同的功能?没有使用reflect
或任何昂贵的东西?
你的问题还不太清楚。请澄清“很明显,这是不可能的”的意思。 – nos
“this”表示我上面的代码为我抛出错误。编辑的问题更清晰。 – Benjam