Standard library
Go does not provide an HTTP session manager. Thus, you must write one or use one written by others.
Some examples:
Typically, HTTP sessions are managed using cookies between the server and the client, and therefore the session (session ID) can be obtained directly from the request ( http.Request ), for example with Request.Cookie() .
There is no need to "pass" the session through the "request chain", each handler can access it only using http.Request .
For example, using github.com/icza/session , this can be done as follows:
func MyHandler(w http.ResponseWriter, r *http.Request) { sess := session.Get(r) if sess == nil { // No session (yet) } else { // We have a session, use it } }
Using Gorilla sessions seems to be:
var store = sessions.NewCookieStore([]byte("something-very-secret")) func MyHandler(w http.ResponseWriter, r *http.Request) { session, err := store.Get(r, "session-name") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }
icza source share