基于简单使用,易扩展的目的!提供了多存储的支持,内存存储、文件存储、redis 存储、cookie 存储,后续后增加 mysql、mongodb 的支持。以及多种中间件的支持,有 gin、echo、beego、gear。欢迎来踩!!!
下面给出一个使用示例:
package main import ( "context" "fmt" "net/http" "github.com/go-session/session" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { store, err := session.Start(context.Background(), w, r) if err != nil { fmt.Fprint(w, err) return } store.Set("foo", "bar") err = store.Save() if err != nil { fmt.Fprint(w, err) return } http.Redirect(w, r, "/foo", 302) }) http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { store, err := session.Start(context.Background(), w, r) if err != nil { fmt.Fprint(w, err) return } foo, ok := store.Get("foo") if ok { fmt.Fprintf(w, "foo:%s", foo) return } fmt.Fprint(w, "does not exist") }) http.ListenAndServe(":8080", nil) }
1 FrankAdler 2018-06-14 13:05:49 +08:00 mark 一下, 现在都是在写无状态 api, 暂时用不到 session |
![]() | 2 my3157 2018-06-14 13:15:41 +08:00 也是 rest api , 把 go-macaron/cache 魔改了 处理 access token |