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  gopkg.in/boj/redistore.v1 and github.com/gorilla/sessions using the go get command, as follows:
$ go get gopkg.in/boj/redistore.v1$ go get github.com/gorilla/sessions
  1. Create http-session-redis.go, where we will create a RedisStore to store and retrieve session variables, as follows:
package mainimport (  "fmt"  "log"  "net/http"  "github.com/gorilla/sessions"  redisStore "gopkg.in/boj/redistore.v1")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")var store *redisStore.RediStorevar err errorfunc init() {  store, err = redisStore.NewRediStore(10, "tcp", ":6379", "",  []byte("secret-key"))  if err != nil   {    log.Fatal("error getting redis store : ", err)  }}func home(w http.ResponseWriter, r *http.Request) { session, _ := ...

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