Skip to content

Instantly share code, notes, and snippets.

@jaybill
Created May 2, 2012 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaybill/2577424 to your computer and use it in GitHub Desktop.
Save jaybill/2577424 to your computer and use it in GitHub Desktop.
gorilla sessions issue
package main
import (
"net/http"
"code.google.com/p/gorilla/sessions"
"log"
)
var store = sessions.NewCookieStore([]byte("something-very-secret"))
type User struct{
Id int64
Username string
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
// Get a session. We're ignoring the error resulted from decoding an
// existing session: Get() always returns a session, even if empty.
session, _ := store.Get(r, "session-name")
// Set some session values.
user := User{Id: 3, Username: "someguy"}
session.Values["user"] = user
session.Values["foo"] = "bar"
session.Save(r, w)
}
func AnotherHandler(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "session-name")
if err != nil{
log.Println(err)
}
log.Printf("Session vals: %+v", session.Values)
}
// If you hit localhost:8080/my and then localhost:8080/another, the session is just an empty map
func main(){
http.HandleFunc("/my",MyHandler)
http.HandleFunc("/another",AnotherHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment