golang gorilla sessions 注意

1.  注意對session的values作改變,必須save才能生效git

        //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基本類型外,複雜對象結構存儲,必須先註冊github

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{})
	}
相關文章
相關標籤/搜索