2

我似乎无法弄清楚为什么自定义结构没有使用 encoding/gob 注册

当我尝试保存 Gorilla 会话时会发生此错误:

could not save session: securecookie: error - caused by: securecookie: error - caused by: gob: type not registered for interface: []*baseball.Athlete

这是结构

type Athlete struct {
    AuthID      string
    DisplayName string
    Email       string
    Coach       string
}

我正在使用 init() 函数注册结构

gob.Register(&baseball.Athlete{})

在身份验证函数中,我尝试保存包含实体的会话。

athlete := make([]*baseball.Athlete, 0)
_, err = q.GetAll(ctx, &athlete)

if len(athlete) == 0 {

    athlete := &baseball.Athlete{
        AuthID:      id_token.Sub, // I'm using id_token.sub which is unigue to Google, not sure if other Oauth2 providers have equivalent
        Email:       id_token.Email,
    }

    _, err := datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "Athlete", nil), athlete)
    if err != nil {
        return appErrorf(err, "authgo: new Athlete put failed: %v", err)
    }
}

oauthFlowSession.Values[currentAthleteKey] = athlete

log.Printf("$#$#$#$#$#$2 in auth.go athlete is type: %T", athlete)
if err := oauthFlowSession.Save(r, w); err != nil {
    return appErrorf(err, "could not save session: %v", err)
}

这是错误的服务器日志:

2019/07/24 21:05:56 athlete is type []*baseball.Athlete
2019/07/24 21:05:56 Handler error: status code: 500, message: could not save session: securecookie: error - caused by: securecookie: error - caused by: gob: type not registered for interface: []*baseball.Athlete, underlying err: securecookie.MultiError{securecookie.cookieError{typ:1, msg:"", cause:securecookie.cookieError{typ:1, msg:"", cause:(*errors.errorString)(0xc4201f8a60)}}}

不同的变体:
gob.Register([]*baseball.Athlete) ==> 错误:类型 []*baseball.Athlete 不是表达式

附带问题,你会怎么说 "[]*baseball.Athlete" ?

我猜,“切片是指向 structball.Athlete 的指针”

4

0 回答 0