title: Best practice with sessions (gorilla/sessions)
body: <p>Before starting using sessions in golang I need answers to some questions</p>
  <h1>session example</h1>
  <pre><code>import &quot;github.com/gorilla/sessions&quot;
  
  var store = sessions.NewCookieStore([]byte(&quot;33446a9dcf9ea060a0a6532b166da32f304af0de&quot;))
  
  func Handler(w http.ResponseWriter, r *http.Request){
      session, _ := store.Get(r, &quot;session-name&quot;)
      
      session.Values[&quot;foo&quot;] = &quot;bar&quot;
      session.Values[42] = 43
      session.Save(r, w)
      
      fmt.Fprint(w, &quot;Hello world :)&quot;)
  }
  
  func main(){
      store.Options = &amp;sessions.Options{
          Domain:     &quot;localhost&quot;,
          Path:       &quot;/&quot;,
          MaxAge:     60 * 15,
          Secure:     false,
          HttpOnly:   true,
      }
  }
  </code></pre>
  <h1>Q1:</h1>
  <p>Is it possible to add multiple sessions on the same domain with different names?</p>
  <pre><code>session1, _ := store.Get(r, &quot;session-name-1&quot;)
  session2, _ := store.Get(r, &quot;session-name-2&quot;)
  </code></pre>
  <p>When do you need multiple sessions on the same domain?</p>
  <h1>Q2:</h1>
  <p>What is the best practice to get the variables from the session?
  my_session_var = session.Values[&quot;foo&quot;]</p>
  <h1>Q3:</h1>
  <p>How to check if the session is saved correctly? If you access the same <code>map</code> to both set and get variables?</p>
  <h1>update</h1>
  <pre><code>package main
  
  import (
      &quot;github.com/gorilla/sessions&quot;
  )
  
  var (
      store = sessions.NewCookieStore([]byte(&quot;33446a9dcf9ea060a0a6532b166da32f304af0de&quot;))
  )
  
  type handler func(w http.ResponseWriter, r *http.Request, s *sessions.Session)
  
  func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request){
      session, _ := store.Get(r, &quot;session-name&quot;)
      
      h(w, r, session)
  }
  
  func Handler_404(w http.ResponseWriter, r *http.Request, s *sessions.Session){
      fmt.Fprint(w, &quot;Oops, something went wrong!&quot;)
  }
  </code></pre>
  <p>error</p>
  <pre><code># command-line-arguments
  .\mux.go:101: cannot convert Handler_404 (type func(http.ResponseWriter, *http.Request, *sessions.Session)) to type http.HandlerFunc
  </code></pre>