From 0f44a27391116bbb840afe7ee861d0f0d0302a1b Mon Sep 17 00:00:00 2001 From: rcadena Date: Wed, 11 Feb 2015 09:37:20 -0800 Subject: Added note about calling save before writing to response. Also removed calls to fmt.Fprintf in AddFlash example. If a user follows the old example then the session won't be stored. See: https://github.com/gorilla/sessions/issues/39 --- doc.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'doc.go') diff --git a/doc.go b/doc.go index 7f8be22..c4012af 100644 --- a/doc.go +++ b/doc.go @@ -56,6 +56,9 @@ is to wrap the top-level mux when calling http.ListenAndServe: The ClearHandler function is provided by the gorilla/context package. +Also: Call Save before writing to the response, otherwise the session +cookie will not be sent to the client. + That's all you need to know for the basic usage. Let's take a look at other options, starting with flash messages. @@ -69,12 +72,10 @@ flashes, call session.Flashes(). Here is an example: session, _ := store.Get(r, "session-name") // Get the previously flashes, if any. if flashes := session.Flashes(); len(flashes) > 0 { - // Just print the flash values. - fmt.Fprint(w, "%v", flashes) + // Use the flash values. } else { // Set a new flash. session.AddFlash("Hello, flash messages world!") - fmt.Fprint(w, "No flashes found.") } session.Save(r, w) } -- cgit v1.2.3 From 2c775edb25fb0f91965caac0120a59ba80858c89 Mon Sep 17 00:00:00 2001 From: rcadena Date: Wed, 11 Feb 2015 09:47:56 -0800 Subject: Moved and tweaked text. --- doc.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'doc.go') diff --git a/doc.go b/doc.go index c4012af..b56700f 100644 --- a/doc.go +++ b/doc.go @@ -48,6 +48,9 @@ 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. +Save must be called before writing to the response, otherwise the session +cookie will not be sent to the client. + Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory! An easy way to do this is to wrap the top-level mux when calling http.ListenAndServe: @@ -56,9 +59,6 @@ is to wrap the top-level mux when calling http.ListenAndServe: The ClearHandler function is provided by the gorilla/context package. -Also: Call Save before writing to the response, otherwise the session -cookie will not be sent to the client. - That's all you need to know for the basic usage. Let's take a look at other options, starting with flash messages. -- cgit v1.2.3