package main import ( "fmt" "net/http" "strconv" ) func GetAndSetCookies(writer http.ResponseWriter, request *http.Request) { counterVal := 1 counterCookie, err := request.Cookie("counter") if err == nil { counterVal, _ = strconv.Atoi(counterCookie.Value) counterVal++ } http.SetCookie(writer, &http.Cookie{ Name: "counter", Value: strconv.Itoa(counterVal), }) if len(request.Cookies()) == 0 { fmt.Fprintln(writer, "Request contains no cookies") } else { for _, cookie := range request.Cookies() { fmt.Fprintf(writer, "Cookie Name: %s , Value: %s \n", cookie.Name, cookie.Value) } } fmt.Fprintf(writer, "Total number of cookies: %d counter Value: %v", len(request.Cookies()), counterVal) } func init() { http.HandleFunc("/cookies", GetAndSetCookies) }