版权声明:所有的博客都是作为个人笔记的。。。。。。 https://blog.csdn.net/qq_35976351/article/details/82633547
一般的写入和读取就使用下面的方式:
import (
"fmt"
"net/http"
)
func setCookueHandler(w http.ResponseWriter, r *http.Request) {
c1 := http.Cookie{
Name: "first_cookie",
Value: "I am first",
HttpOnly: true,
}
c2 := http.Cookie{
Name: "second_cookie",
Value: "I am second",
HttpOnly: true,
}
// 把cookie写入客户端
http.SetCookie(w, &c1)
http.SetCookie(w, &c2)
}
func getCookieHandler(w http.ResponseWriter, r *http.Request) {
// 读取cookie
cookie, _ := r.Cookie("first_cookie")
fmt.Println(cookie)
cookie, _ = r.Cookie("second_cookie")
fmt.Println(cookie)
// 另一种方式读取
for cookie := range r.Cookies() {
fmt.Println(cookie)
}
}