A efficient, safely and easy-to-use session library for Go.
go get -v github.com/go-session/session/v3
package main
import (
"context"
"fmt"
"net/http"
session "github.com/go-session/session/v3"
)
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)
}
go build server.go
./server
foo:bar
- Easy to use
- Multi-storage support
- Multi-middleware support
- More secure, signature-based tamper-proof
- Context support
- Support request header and query parameters
- https://github.com/go-session/redis - Redis
- https://github.com/go-session/mongo - MongoDB
- https://github.com/go-session/gorm - GORM
- https://github.com/go-session/mysql - MySQL
- https://github.com/go-session/buntdb - BuntDB
- https://github.com/go-session/cookie - Cookie
- https://github.com/go-session/gin-session - Gin
- https://github.com/go-session/beego-session - Beego
- https://github.com/go-session/gear-session - Gear
- https://github.com/go-session/echo-session - Echo
Copyright (c) 2021 Lyric