Well, session.Save will return error. you can get info on the error by printing or acting on it.
There was an error saying <gob: type main.Person has no exported fields>.
So, I changed both Person & Monster to what it looks in the following. Just make sure the required fields are exported so that gob can access the fields which you need to save in session.
package main
import (
"encoding/gob"
"fmt"
"net/http"
)
var store = sessions.NewCookieStore(securecookie.GenerateRandomKey(32))
func main() {
sessions.NewSession(store, "session-name")
http.HandleFunc("/1", MyHandler1)
http.HandleFunc("/2", MyHandler2)
http.ListenAndServe(":8080", nil)
}
type Person struct {
Name string
}
type Monster struct {
Name string
}
func init() {
gob.Register(&Person{})
gob.Register(&Monster{})
}
func MyHandler1(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
session.Values["person"] = &Person{"John Doe"}
session.Values["monster"] = &Monster{"Sasquatch"}
fmt.Println(session.Save(r, w))
}
func MyHandler2(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
fmt.Println(session.Values["person"])
fmt.Println(session.Values["monster"])
}