项目作者: lifei6671

项目描述 :
基于Go实现的Session组件
高级语言: Go
项目地址: git://github.com/lifei6671/gosession.git
创建时间: 2016-05-07T05:51:25Z
项目社区:https://github.com/lifei6671/gosession

开源协议:MIT License

下载


gosession

基于Go实现的Session组件,代码参考了Beego的Session部分。目前实现了基于内存的Session和基于文件的Session。

通过实现SessionHandler和SessionProvider接口,可以扩展更多的储存方式。

使用方法:

  1. http.HandleFunc("/",helloWorld);
  2. config := gosession.SessionConfig{
  3. CookieName : "GO_SESSIONID",
  4. EnableSetCookie : true,
  5. Gclifetime : 10,
  6. Maxlifetime : 10,
  7. Secure : false,
  8. CookieLifeTime : 3600,
  9. ProviderConfig : "/mydata/session",
  10. Domain : "",
  11. }
  12. manager,_ = gosession.NewSessionManager("file",config);
  13. err := http.ListenAndServe(":8000",nil);
  14. if(err != nil){
  15. fmt.Println(err);
  16. }
  17. fmt.Println("应用已启动")

helloWord实现:

  1. func helloWorld(w http.ResponseWriter, r *http.Request) {
  2. r.ParseForm();
  3. session,_ := manager.SessionStart(w,r);
  4. session.Add("abc","dddddddddddddddddddd");
  5. value,_ := session.Get("abc");
  6. fmt.Fprintf(w,"hellow World!",value);
  7. }