【发布时间】:2026-02-17 05:55:01
【问题描述】:
我正在尝试为我的 Kinds 创建一个包装器,这就是我的做法:
package model
import (
"time"
)
type Kind interface {
Name() string
}
type Message struct {
Text string
CreatedOn time.Time
UserId string
}
func (q Message) Name() string {
return "MESSAGE"
}
而我介绍type Kind interface的原因是:
// Stores the given model for the the kind in data store
func Store(req *http.Request, data Kind) error {
ctx := appengine.NewContext(req)
key := datastore.NewKey(ctx, data.Name(), "", 0, nil)
_, err := datastore.Put(ctx, key, &data)
return err
}
如您所见,我使用data.Name() 来获取种类名称。
当我尝试保存数据时,它抱怨:
datastore: invalid entity type
我读到这可能是由于没有传递对datastore.Put 的引用,但我正在这样做。有什么想法吗?
我必须补充一点,当我检查数据类型时(使用reflect.TypeOf()),它也是model.Message,这也是正确的。所以它是一个具体的类型。
【问题讨论】:
标签: google-app-engine go google-cloud-datastore