New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session保存自定义struct后读取出现 gob: name not registered for interface错误 #1110

Closed
cfanboy opened this Issue Apr 3, 2015 · 10 comments

Comments

Projects
None yet
6 participants
@cfanboy
Copy link

cfanboy commented Apr 3, 2015

自定义Account如下:
type Account struct {
Id bson.ObjectId bson:"_id,omitempty" json:"id"
LoginName string bson:"loginName" json:"loginName"
Password string bson:"password" json:"password"
UserName string bson:"userName" json:"userName"
}

在controller中使用SetSession方法保存登录后的用户,第一次登录没问题,但登录后再次刷新界面或跳转到Login界面,就会出现错误:
[router.go:650] [E] gob: name not registered for interface: "*models.Account"

清除浏览器端Cookie或删除Session目录中的内容后又可以正常登录

@astaxie

This comment has been minimized.

Copy link
Owner

astaxie commented Apr 3, 2015

恩,这是一个已知bug,而且目前无法优雅的解决,你可以通过这样的方式解决:

在你的main.go里面,init函数里面使用gob.Register你的自定义的struct

@cfanboy

This comment has been minimized.

Copy link
Author

cfanboy commented Apr 6, 2015

ok,已经这样注册了,多谢谢大 ^_^

@lei-cao

This comment has been minimized.

Copy link
Contributor

lei-cao commented Dec 30, 2015

@cfanboy Thank you for supporting beego. please update or submit your issue in English for a better international user friendly communication.

@lei-cao

This comment has been minimized.

Copy link
Contributor

lei-cao commented Dec 30, 2015

@astaxie can be close

@JessonChan

This comment has been minimized.

Copy link
Contributor

JessonChan commented Jan 7, 2016

@astaxie can close this issue

@astaxie astaxie closed this Jan 7, 2016

@mytc

This comment has been minimized.

Copy link

mytc commented Aug 1, 2017

Anyone can show me how to use gob in main? I am confused.
I have similar error:

gob: name not registered for interface: "time.Time"

The documentation only mention register the gob in init in main, but I really don't know what to register. I didn't struct to store the session.

Would be better if the documentation give more examples. It's been really hard for golang newbie like me to understand how does this beego work.

@astaxie

This comment has been minimized.

Copy link
Owner

astaxie commented Aug 3, 2017

@mytc what did you store in session?

@mytc

This comment has been minimized.

Copy link

mytc commented Aug 3, 2017

@astaxie, there were two fields, name and expire which is both are string.
I think the problem lays on the expire field.

this.SetSession("expiry", time.Now().Add(time.Minute*4))

I thinks should parse the time, right?

@astaxie

This comment has been minimized.

Copy link
Owner

astaxie commented Aug 6, 2017

time.Now().Add(time.Minute*4) return time.Time, So actually you save this struct. If you want to save the struct, you need to use the gob to register. gob. Register(time.Time{})

@TeriMoni

This comment has been minimized.

Copy link

TeriMoni commented Nov 6, 2017

sessionprovider 改成内存形式就没有这个问题,文件形式会出现,能每次项目重启自动删除本地的session文件么

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment