How to do it...

  1. Install the github.com/gorilla/sessions package using the go get command, as follows:
$ go get github.com/gorilla/sessions
  1. Create http-session.go where we will create a Gorilla cookie store to save and retrieve session information defining three handlers—/login, /home, and /logout—where we will be creating a valid session cookie, writing a response to an HTTP response stream, and invalidating a session cookie respectively, as follows:
package mainimport (  "fmt"  "log"  "net/http"  "github.com/gorilla/sessions")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")var store *sessions.CookieStorefunc init() {  store = sessions.NewCookieStore([]byte("secret-key"))}func home(w http.ResponseWriter, r *http.Request) { session, _ := ...

Get Go Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.