BeeGO框架中 session的简单使用

package controllers

import (
	"github.com/astaxie/beego"
	//(1)导入session包
	"github.com/astaxie/beego/session"
)

//(2)建立一个全局session mananger对象
var globalSessions *session.Manager

//(3)在初始化“全局session mananger对象”
func init() {
	globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":3600}`)
	go globalSessions.GC()
}
func (this *MainController) Get() {

	//(4)获取当前的请求会话,并返回当前请求会话的对象
	sess := globalSessions.SessionStart(this.Ctx.ResponseWriter, this.Ctx.Request)
	//(5)根据当前请求对象,设置一个session
	sess.Set("mySession", "this my session------------")

	this.Data["Website"] = "beego.me"
	//(6)从session中读取值
	this.Data["Email"] = sess.Get("mySession")

	this.TplNames = "index.tpl"
}

type MainController struct {
	beego.Controller
}

如上代码,前三步是准备阶段   后三步是使用阶段

你可能感兴趣的:(Go语言)