Created
February 2, 2015 20:42
-
-
Save hjr265/6805ec1736b5cd4416f5 to your computer and use it in GitHub Desktop.
Using "gorilla/sessions"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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