title: How to delete cookie
body: <p>I wrote a web application that set a cookie and delete it. To clarify to scenario what I mean look at the following code snippet.</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "github.com/gorilla/mux"
      "net/http"
      "time"
  )
  
  func rootHandler(rw http.ResponseWriter, r *http.Request) {
      fmt.Fprintf(rw, "Hello Foo")
  
  }
  
  func setCookieHandler(rw http.ResponseWriter, r *http.Request) {
      c := &amp;http.Cookie{
          Name:     "storage",
          Value:    "value",
          Path:     "/",
          MaxAge:   0,
          HttpOnly: true,
      }
  
      http.SetCookie(rw, c)
  }
  
  func deleteCookieHandler(rw http.ResponseWriter, r *http.Request) {
  
      c, err := r.Cookie("storage")
      if err != nil {
          panic(err.Error())
      }
      c.Name = "Deleted"
      c.Value = "Unuse"
      c.Expires = time.Unix(1414414788, 1414414788000)
  }
  
  func readCookieHandler(rw http.ResponseWriter, r *http.Request) {
  
      c, err := r.Cookie("storage")
      if err != nil {
          panic(err.Error())
      }
      fmt.Println(c.Expires)
  }
  
  func evaluateCookieHandler(rw http.ResponseWriter, r *http.Request) {
  
      c, err := r.Cookie("storage")
      if err != nil {
          panic(err.Error())
      }
  
      if time.Now().After(c.Expires) {
          fmt.Println("Cookie is expired.")
      }
  }
  
  func main() {
      mux := mux.NewRouter()
      mux.HandleFunc("/", rootHandler)
      mux.HandleFunc("/cookie", setCookieHandler)
      mux.HandleFunc("/delete", deleteCookieHandler)
      mux.HandleFunc("/read", readCookieHandler)
      mux.HandleFunc("/eval", evaluateCookieHandler)
  
      http.ListenAndServe(":3000", mux)
  }
  </code></pre>
  
  <p>As you can see, when I visit /cookie location, it will be set a cookie as expected. Then when I call /delete, it should change the name, value and expired time from cookie. The expired time is changed, but name and value not.  </p>
  
  <p><img src="https://i.stack.imgur.com/vSkph.png" alt="enter image description here"></p>
  
  <p>What do I want is, to delete the cookie from browser for sign out in a authentication system, when user click sign out button to delete cookie.<br>
  I also discover <a href="https://stackoverflow.com/questions/5285940/correct-way-to-delete-cookies-server-side">this link</a> and follow the advice, but does not work as expected.</p>