Gorilla网络工具包:len(session.Flashes())为0

I have a register form I'm trying to put together, with simple logic I have form validation checked on the server side with simple ifs e.g.

if username == "" || < 5 {
    session.AddFlash("Username is too short")
    session.Save(r, w)
}

That works fine then at the end of the validation I do (for debugging purposes)

fmt.Println(len(session.Flashes())) which returns 3

Then I check to see if there were any form errors like so

if len(session.Flashes()) != 0 {
    // Perform Redirect and show flashes
} else {
    // Set proper session variables and log user in
}

So if 3 > 0 why is the else statement being triggered and not the first part of the if statement? I'm just unsure why this is happening the way it is. Thanks if you need anymore information let me know, actual code snippet:

if username == "" || len(username) < 4 {
    session.AddFlash("Username is too short")
    session.Save(r, w)
}
if email == "" || len(email) < 5 {
    session.AddFlash("Email is too short")
    session.Save(r, w)
}
if firstname == "" || len(firstname) < 3 {
    session.AddFlash("Firstname is too short")
    session.Save(r, w)
}
if lastname == "" || len(lastname) < 3 {
    session.AddFlash("Lastname is too short")
    session.Save(r, w)
}

fmt.Println(len(session.Flashes()) > 0) // true

if len(session.Flashes()) != 0 {
    fmt.Println("Why am I here also")
    type Page struct {
        Title    string
        Username interface{}
        Errors   []interface{}
    }

    session, _ := common.GetSession(r)
    data := &Page{"Register", session.Values["username"], session.Flashes()}
    session.Save(r, w)

    tmpl, err := template.ParseFiles("views/register/register.html")
    if err != nil {
        http.Error(w, "Failed to load page.", 500)
    }
    tmpl.ExecuteTemplate(w, "register", data)
} else {
    fmt.Println("Why am I here")
    _, err := db.Query("// Perform DB Query")
    if err != nil {
        http.Error(w, "Sorry we had trouble saving your account to the database, try again in a bit.", 500)
    }

    session.Values["username"] = r.FormValue("username")
    session.Values["authenticated"] = true

    session.Save(r, w)

    http.Redirect(w, r, "/", 303)
}

It's not very well documented, but apparently Flashes removes flashes from the session and returns them:

func (s *Session) Flashes(vars ...string) []interface{} {
    var flashes []interface{}
    key := flashesKey
    if len(vars) > 0 {
        key = vars[0]
    }
    if v, ok := s.Values[key]; ok {
        // Drop the flashes and return it.
        delete(s.Values, key)
        flashes = v.([]interface{})
    }
    return flashes
}

Source code here.

The solution here is to use a separate variable to save the validation state:

valid := true
if username == "" || len(username) < 4 {
    valid = false
    session.AddFlash("Username is too short")
    session.Save(r, w)
}
// ...
if !valid {
    // ...
} else {
    // ...
}

EDIT: Another way to get flashes without deleting them is to get them from Values directly:

flashes := session.Values["_flash"].([]interface{})