golang gorilla sessions 注意
1. 注意对session的values做改变,必须save才能生效
//login session, _ := store.Get(ctx.Request, "sessionID") timeString := time.Now().Format(common.DefaultMsTimeLayout) session.Values["login_time"] = timeString session.Save(ctx.Request, ctx)
//logout for key, _ := range session.Values { delete(session.Values, key) } session.Save(ctx.Request, ctx) //删除session内属性也需要save
2. 除go基本类型外,复杂对象结构存储,必须先注册
There may also be cases where you want to store a complex datatype within a session, such as a struct. Sessions are serialised using the encoding/gob package, so it is easy to register new datatypes for storage in sessions: import( "encoding/gob" "github.com/gorilla/sessions" ) type Person struct { FirstName string LastName string Email string Age int } type M map[string]interface{} func init() { gob.Register(&Person{}) gob.Register(&M{}) }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。