Beego Session使用
Beego Session使用
import (
"github.com/astaxie/beego/session"
)
// 初始化一个全局的变量用来存储 session 控制器
var globalSessions *session.Manager
//入口函数中初始化数据
func init() {
globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid", "enableSetCookie,omitempty": true, "gclifetime":3600, "maxLifetime": 3600, "secure": false, "sessionIDHashFunc": "sha1", "sessionIDHashKey": "", "cookieLifeTime": 3600, "providerConfig": ""}`)
go globalSessions.GC()
}
func login(w http.ResponseWriter, r *http.Request) {
sess, _ := globalSessions.SessionStart(w, r)
defer sess.SessionRelease(w)
username := sess.Get("username")
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
} else {
sess.Set("username", r.Form["username"])
}
}
【励志篇】:
古之成大事掌大学问者,不惟有超世之才,亦必有坚韧不拔之志。