Q:

Gorilla session.AddFlash Does Not Add Flash Message

I have a registration page with two handlers, one for displaying the form, one for processing a form submission.
I am trying to use a session.AddFlash method to save an error, then do 302 redirect back to the registration form and display the error.
I set up a session store:
package web

import (
    "github.com/gorilla/sessions"
)

var sessionStore = sessions.NewCookieStore([]byte(sessionSecret))

Then my handlers look like this:
package web

import (
    "html/template"
    "log"
    "net/http"
)

func registerForm(w http.ResponseWriter, r *http.Request) {
    session, err := sessionStore.Get(r, "mysession")
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    data := map[string]interface{}{}
    log.Print("Flashes: ")
    log.Print(session.Flashes())
    if flashes := session.Flashes(); len(flashes) > 0 {
        data["error"] = flashes[0]
    }

    tmpl, _ := template.ParseFiles("web/templates/register.html.tmpl")
    tmpl.Execute(w, data)
}

func register(w http.ResponseWriter, r *http.Request) {
    session, err := sessionStore.Get(r, "mysession")
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    r.ParseForm()
    username := r.Form["username"][0]
    password := r.Form["password"][0]

    if UserExists(username) {
        log.Print("Username already taken")
        session.AddFlash("Username already taken")
        http.Redirect(w, r, "/web/register", http.StatusFound)
        return
    }

    _, err = CreateUser(username, password)
    log.Print(err)

    if err != nil {
        session.AddFlash(err.Error())
        http.Redirect(w, r, "/web/register", http.StatusFound)
        return
    }

    http.Redirect(w, r, "/web/login", http.StatusFound)
}

By adding logs I can see that UserExists returns true therefor a flash message should be added however after redirection to the form handler there is no flash message saved in the session.

A:

I think you have to save the session before you redirect.
session.Save(r, w)
http://www.gorillatoolkit.org/pkg/sessions#Session.Save

