![License](https://camo.githubusercontent.com/1258d2285d00884260b631cd83215469465e212cc6fb3aa0c37319f437a0eb3e/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f6c2f657870726573732e737667)
$ go get -u -v github.com/go-session/mysql
package main
import (
"context"
"fmt"
"net/http"
"github.com/go-session/mysql"
"github.com/go-session/session"
_ "github.com/go-sql-driver/mysql"
)
func main() {
dsn := "root:123456@tcp(127.0.0.1:3306)/myapp_test?charset=utf8"
session.InitManager(
session.SetStore(mysql.NewDefaultStore(mysql.NewConfig(dsn))),
)
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
http://localhost:8080