Skip to content

Instantly share code, notes, and snippets.

@hjr265
Created February 2, 2015 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjr265/6805ec1736b5cd4416f5 to your computer and use it in GitHub Desktop.
Save hjr265/6805ec1736b5cd4416f5 to your computer and use it in GitHub Desktop.
Using "gorilla/sessions"
package main
import (
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
)
func main() {
store := sessions.NewCookieStore([]byte("something-very-secret"))
r := mux.NewRouter()
r.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
session.Values["myvalue"] = r.FormValue("myvalue")
store.Save(r, w, session)
})
r.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
w.Write([]byte(session.Values["myvalue"].(string)))
})
http.ListenAndServe(":8080", r)
}
func catch(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment