




$ go get -u -v github.com/go-session/buntdb
package main
import (
"context""fmt""net/http""github.com/go-session/buntdb""github.com/go-session/session"
)
funcmain() {
session.InitManager(
session.SetStore(buntdb.NewFileStore("session.db")),
)
http.HandleFunc("/", func(w http.ResponseWriter, r*http.Request) {
store, err:=session.Start(context.Background(), w, r)
iferr!=nil {
fmt.Fprint(w, err)
return
}
store.Set("foo", "bar")
err=store.Save()
iferr!=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)
iferr!=nil {
fmt.Fprint(w, err)
return
}
foo, ok:=store.Get("foo")
ifok {
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