预期指向结构切片的指针

huangapple go评论87阅读模式
英文:

Expected pointer to struct slice

问题

我正在使用golang编写一个服务器。

我有一个auth-helper,用安全令牌对用户进行身份验证(这只是一个测试)。当我在AuthUsingCredentials函数中进行查询时(我正在使用这个dep),它输出以下错误:

"Expected pointer to struct slice *[]struct"

如果我将var result *entities.User更改为var result []entities.User,然后打印result[0],它可以工作,但是然后会输出:

"cannot use result[0] (type entities.User) as type *entities.User in argument to ah.userEntityToModel"

auth helper的代码:

auth.go

package helpers

import (
  "fmt"

  "server/data/entities"
  "server/data/models"
  "server/interfaces"
)

var AuthHelper *authHelper

type authHelper struct{}

func init() {
	AuthHelper = &authHelper{}
}

func (ah *authHelper) userEntityToModel(_entity *entities.User) (*models.User, error) {
  u := models.NewUser(_entity, db)
	u.Username = _entity.Username

  return u, nil
}

func (ah *authHelper) AuthenticateUsingCredentials(_token string) (interfaces.IUser, error) {
  var result *entities.User
  err := db.Where("Token", "=", _token).Find(result)
  if err != nil {
    return nil, err
  }
  if result == nil {
    return nil, fmt.Errorf("Player '%s' not found", _token)
  }

  playerModel, err := ah.userEntityToModel(result)
  if err != nil {
    return nil, err
  }

  return playerModel, nil
}
英文:

I'm working on a server in golang.

I have an auth-helper that authenticates the user with a secure token (its a test). The error comes when i make a query (I'm using this dep) in the AuthUsingCredentials function, it outputs the following error:

> "Expected pointer to struct slice *[]struct"

If I change the var result *entities.User for var result []entities.User and then print result[0] it works but then it outputs:

> "cannot use result[0] (type entities.User) as type *entities.User in
> argument to ah.userEntityToModel"

The auth helper code:

auth.go

package helpers

import (
  "fmt"

  "server/data/entities"
  "server/data/models"
  "server/interfaces"
)

var AuthHelper *authHelper

type authHelper struct{}

func init() {
	AuthHelper = &authHelper{}
}

func (ah *authHelper) userEntityToModel(_entity *entities.User) (*models.User, error) {
  u := models.NewUser(_entity, db)
	u.Username = _entity.Username

  return u, nil
}

func (ah *authHelper) AuthenticateUsingCredentials(_token string) (interfaces.IUser, error) {
  var result *entities.User
  err := db.Where("Token", "=", _token).Find(result)
  if err != nil {
    return nil, err
  }
  if result == nil {
    return nil, fmt.Errorf("Player '%s' not found", _token)
  }

  playerModel, err := ah.userEntityToModel(result)
  if err != nil {
    return nil, err
  }

  return playerModel, nil
}

答案1

得分: 0

应该可以这样做。

func (ah *authHelper) AuthenticateUsingCredentials(_token string) (interfaces.IUser, error) {
  var result []entities.User
  err := db.Where("Token", "=", _token).Find(&result)
  if err != nil {
    return nil, err
  }
  if len(result) <= 0 {
    return nil, fmt.Errorf("Player '%s' not found", _token)
  }

  playerModel, err := ah.userEntityToModel(&result[0])
  if err != nil {
    return nil, err
  }

  return playerModel, nil
}

你试图将一个用户切片放入一个接受单个用户指针的函数中。

英文:

Something like this should work.

func (ah *authHelper) AuthenticateUsingCredentials(_token string) (interfaces.IUser, error) {
  var result *[]entities.User
  err := db.Where(&quot;Token&quot;, &quot;=&quot;, _token).Find(result)
  if err != nil {
    return nil, err
  }
  if len(result) &lt;= 0 {
    return nil, fmt.Errorf(&quot;Player &#39;%s&#39; not found&quot;, _token)
  }

  playerModel, err := ah.userEntityToModel(&amp;(*result)[0])
  if err != nil {
    return nil, err
  }

  return playerModel, nil
}

you are trying to put an slice of users in a function that takes a pointer to a single user.

huangapple
  • 本文由 发表于 2015年4月26日 06:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/29871354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定