Use go gin to operate the cookie tutorial

  • 2020-06-23 00:39:39
  • OfStack

To be precise, the title is problematic. go gin can only give the browser instructions to operate on cookie. It's the browser that does the actual cookie operation. But broadly speaking, it is possible to say go gin operates cookie (indirectly)

Look at the go gin code:


package main
import (
  "github.com/gin-gonic/gin"
)
func main() {
  router := gin.Default();
  router.GET("/read_cookie", func(context *gin.Context) {
    val, _ := context.Cookie("name")
    context.String(200, "Cookie:%s", val) 
  })
  router.GET("/write_cookie", func(context *gin.Context) {
    context.SetCookie("name", "Shimin Li", 10, "/", "localhost", false, true)
  })
  router.GET("/clear_cookie", func(context *gin.Context) {
    context.SetCookie("name", "Shimin Li", -1, "/", "localhost", false, true)
  })
  router.Run(":8080")
}

Start the service. The operation of reading, writing and clearing performed by the browser side is as follows:

http://localhost:8080/read_cookie

http://localhost:8080/write_cookie

http://localhost:8080/clear_cookie

I played OK 1 time.

Not much said.

conclusion


Related articles: