O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

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. Open the console and create the folder chapter09/recipe10.
  2. Navigate to the directory.
  3. Create the file cookies.go with the following content:
        package main        import (          "fmt"          "log"          "net/http"          "time"        )        const cookieName = "X-Cookie"        func main() {          log.Println("Server is starting...")          http.HandleFunc("/set", func(w http.ResponseWriter,                          r *http.Request) {            c := &http.Cookie{              Name: cookieName,              Value: "Go is awesome.",              Expires: time.Now().Add(time.Hour),              Domain: "localhost",            }            http.SetCookie(w, c)            fmt.Fprintln(w, "Cookie is set!")          })          http.HandleFunc("/get", func(w http.ResponseWriter,                          r *http.Request) {            val, err := r.Cookie(cookieName)            if err != nil {              fmt.Fprintln(w, "Cookie err: "+err.Error())              return            } fmt.Fprintf(w, "Cookie is: %s ...

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