aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Silverlock <matt@eatsleeprepeat.net>2013-08-02 10:44:22 +0400
committerMatt Silverlock <matt@eatsleeprepeat.net>2013-08-02 10:44:22 +0400
commit696523391f8287795f3d369d66eb83cd50381880 (patch)
tree10c7c73035ca9bd7f5cb4a6535f63aa768012494
parent8593e03f3101a7001cd6c76a6fb0f3c5cbf2724c (diff)
Added examples for serialising custom types and handling errors from session.Save().
-rw-r--r--doc.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/doc.go b/doc.go
index 1bf3b3c..24ba077 100644
--- a/doc.go
+++ b/doc.go
@@ -45,6 +45,9 @@ store.Get() to retrieve an existing session or a new one. Then we set some
session values in session.Values, which is a map[interface{}]interface{}.
And finally we call session.Save() to save the session in the response.
+Note that in production code, we should check for errors when calling
+session.Save(r, w), and either display an error message or otherwise handle it.
+
That's all you need to know for the basic usage. Let's take a look at other
options, starting with flash messages.
@@ -71,6 +74,36 @@ flashes, call session.Flashes(). Here is an example:
Flash messages are useful to set information to be read after a redirection,
like after form submissions.
+There may also be cases where you want to store a complex datatype within a
+session, such as a struct. Sessions are serialised using the encoding/gob package,
+so it is easy to register new datatypes for storage in sessions:
+
+ import(
+ "encoding/gob"
+ "github.com/gorilla/sessions"
+ )
+
+ type Person struct {
+ FirstName string
+ LastName string
+ Email string
+ Age int
+ }
+
+ type M map[string]interface{}
+
+ func init() {
+
+ gob.Register(&Person{})
+ gob.Register(&M{})
+ }
+
+As it's not possible to pass a raw type as a parameter to a function, gob.Register()
+relies on us passing it an empty pointer to the type as a parameter. In the example
+above we've passed it a pointer to a struct and a pointer to a custom type
+representing a map[string]interface. This will then allow us to serialise/deserialise
+values of those types to and from our sessions.
+
By default, session cookies last for a month. This is probably too long for
some cases, but it is easy to change this and other attributes during
runtime. Sessions can be configured individually or the store can be