英文:
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("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
}
you are trying to put an slice of users in a function that takes a pointer to a single user.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论