dousong5161 2015-03-22 08:42
浏览 48
已采纳

数据存储区:获取时无效的实体类型

Hey im trying to get an entity by passing an id ,

But it`s look like im getting a nill pointer I tried to initialize the entity in couple of ways but the result is the same.

I'm trying to create my server as much as i can like this example to-do

what am im missing here ?

type UserManager struct {
    users []*Users
    user  *Users
}

func NewUserManager() *UserManager {
    return &UserManager{}
}

 func (userManager *UserManager) putUser(c appengine.Context, u *Users) (usreRes *Users, err error) {

key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "users", nil), u)
if err != nil {
    return nil, err
 }
c.Debugf("file key inserted :%#v", key)
return u, nil
}


func (userManager *UserManager) getUserById(userKey string, c appengine.Context) (usreRes *Users, err error) {

entity_id_int, err := strconv.ParseInt(userKey, 10, 64)

if err != nil {
    return userManager.user, err
}
k := datastore.NewKey(c, "users", "", entity_id_int, nil)

userRes := new(Users)
err = datastore.Get(c, k, &userRes)

//err = datastore.Get(c, k, &userManager.user)

if err != nil {
    return userManager.user, err
}

//return userManager.user,nil
return userRes, nil
}
  • 写回答

1条回答 默认 最新

  • du8980919 2018-06-27 03:43
    关注

    It's because datastore.Get(c, k, "pointer") expects a pointer to struct as its third argument, and you are passing a pointer of a pointer **userRes

    userRes := new(Users)
    

    There you are creating a pointer to struct see

    So you should do:

    datastore.Get(c, k, userRes)
    

    Without the ampersand, because with new keyword you are already creating a pointer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序