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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题