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{})
}