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/securecookie package using the go get command, as follows:
$ go get github.com/gorilla/securecookie
  1. Create http-cookie.go, where we will create a Gorilla secure cookie to store and retrieve cookies, as follows:
package mainimport (  "fmt"  "log"  "net/http"  "github.com/gorilla/securecookie")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")var cookieHandler *securecookie.SecureCookiefunc init() {  cookieHandler = securecookie.New(securecookie.  GenerateRandomKey(64),  securecookie.GenerateRandomKey(32))}func createCookie(w http.ResponseWriter, r *http.Request) {  value := map[string]string  {    "username": "Foo",  }  base64Encoded, err := cookieHandler.Encode("key", value)  if err == nil   { cookie := &http.Cookie ...

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