大猩猩会话从会话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:}

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)