douqian3712 2016-08-01 03:58
浏览 32

大猩猩会话从会话golang中提取结构值

I created a user entry in my gorilla session

session.Values["ub"] = ub

However when I am extracting, the extracted interface is getting the value but when I am type-asserting the data into my user structure it is showing null.

I am not getting the reason behind this, as when I am running the same code with other structures, it is working fine. What could be the reason and how to fix this.

val := session.Values["ub"]
    var ub = &UserBasic{}
    var ok bool

    if val != "" {
        //type assertion
        //var userBasic = &auth.UserBasic{}
        ub, ok = val.(*UserBasic)
        if !ok {
            // Handle the case that it's not an expected type
            log.Infof(ctx, "GetUserGSession:  UserBasic structure value %+v", ub)
            log.Infof(ctx, "GetUserGSession:  Value got from session %+v", val)
            log.Infof(ctx, "GetUserGSession:  reasserting  %+v", val.(UserBasic))
        }
        return ub
    } else {
        ub := new(UserBasic)
        return ub
    }

*********Outcome************

2016/08/01 03:49:12 INFO: GetUserGSession:  UserBasic structure value <nil>
2016/08/01 03:49:12 INFO: GetUserGSession:  Value got from session {UserID:589578337 UserName:ds Name:ds createdAt:{sec:0 nsec:0 loc:<nil>} defaultProfileImage:false description: FavouritesCount:5 FollowersCount:0 FriendsCount:0 TotalTweets:6 ListedCount:0 Location: ProfileBgColor: ProfileBgImgURL: ProfileImgURL: IsLoggedIn:true TimeZone:}
2016/08/01 03:49:12 INFO: GetUserGSession:  reasserting  {UserID:589578337 UserName:ds Name:ds createdAt:{sec:0 nsec:0 loc:<nil>} defaultProfileImage:false description: FavouritesCount:5 FollowersCount:0 FriendsCount:0 TotalTweets:6 ListedCount:0 Location: ProfileBgColor: ProfileBgImgURL: ProfileImgURL: IsLoggedIn:true TimeZone:}
  • 写回答

1条回答 默认 最新

  • 普通网友 2016-08-01 09:49
    关注

    If your interface method has pointer receiver you should create it as a pointer, like this:

    var val Worker = &UserBasic{100}
    

    and if you use var val Worker = UserBasic{100} you will see this error:

    UserBasic does not implement Worker (Work method has pointer receiver)
    

    in this working sample code:

    package main
    
    import "fmt"
    
    var _ Worker = (*UserBasic)(nil) // Verify that *UserBasic implements Worker.
    
    func main() {
        var val Worker = &UserBasic{100}
    
        ub, ok := val.(*UserBasic)
    
        fmt.Println(ub, ok) // &{100} true
    }
    
    type Worker interface {
        Work()
    }
    
    type UserBasic struct {
        A int
    }
    
    func (t *UserBasic) Work() {
        fmt.Println("Work.")
    }
    

    you may check at compile-time if a type implements an interface or not:

    Verify that *UserBasic implements Worker (Compile: Success):

    package main
    
    import "fmt"
    
    var _ Worker = (*UserBasic)(nil) // Verify that *UserBasic implements Worker.
    
    func main() {
        fmt.Println("Hello World!")
    }
    
    type Worker interface {
        Work()
    }
    
    type UserBasic struct{}
    
    func (t *UserBasic) Work() {
        fmt.Println("Work.")
    }
    

    Verify that UserBasic implements Worker (Compile: Success):

    package main
    
    import "fmt"
    
    var _ Worker = UserBasic{} // Verify that UserBasic implements Worker.
    
    func main() {
        fmt.Println("Hello World!")
    }
    
    type Worker interface {
        Work()
    }
    
    type UserBasic struct{}
    
    func (t UserBasic) Work() {
        fmt.Println("Work.")
    }
    

    for test sample compile this code (Compile: error):

    package main
    
    import "fmt"
    
    var _ Worker = (*UserBasic)(nil) // Verify that *UserBasic implements Worker.
    
    func main() {
        fmt.Println("Hello World!")
    }
    
    type Worker interface {
        Work()
    }
    
    type UserBasic struct{}
    

    it says:

    .\m.go:5: cannot use (*UserBasic)(nil) (type *UserBasic) as type Worker in assignment:
        *UserBasic does not implement Worker (missing Work method)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?