title: Session middleware for Go?
body: <pre><code>func indexHandler(w http.ResponseWriter, req *http.Request) {
      session, err := store.Get(req, sessionName)
      if err != nil {
          log.WithError(err).Error("bad session")
          http.SetCookie(w, &amp;http.Cookie{Name: sessionName, MaxAge: -1, Path: "/"})
      }
      err = views.ExecuteTemplate(w, "index.html", session.Values)
      if err != nil {
          http.Error(w, err.Error(), http.StatusInternalServerError)
          return
      }
  }
  </code></pre>
  
  <p>All my handlers use <a href="https://github.com/gorilla/sessions" rel="nofollow noreferrer">Gorilla sessions</a>. How can I avoid <code>store.Get</code>ting the session in each of my handlers, i.e. code repeating?</p>
  
  <p>Another issue, is there a better way to give the template the session values, other that an explicit way like:</p>
  
  <pre><code>err = views.ExecuteTemplate(w, "index.html",
      struct {
          Session map[interface{}]interface{},
          ...other handler specific data for the template
      }{
          session.Values,
          ...
      })
  </code></pre>
  
  <p>Code samples originate from <a href="https://github.com/kaihendry/internal-google-login" rel="nofollow noreferrer">https://github.com/kaihendry/internal-google-login</a></p>