O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, _ := store.Get(r, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required