package controller
import (
"fmt"
"net/http"
)
func SayHello(w http.ResponseWriter, req *http.Request) {
//设置cookie 并写出cookie
cookie := http.Cookie{Name: "myCookie", Value: "123456"}
http.SetCookie(w, &cookie)
//获取cookie
if myCookie, err := req.Cookie("myCookie"); err == nil {
fmt.Println(myCookie.Name, ":", myCookie.Value)
}
}
func SayHello2(w http.ResponseWriter, req *http.Request) {
//获取cookie数组
cookies := req.Cookies()
for _, v := range cookies {
fmt.Println(v.Name)
fmt.Println(v.Value)
}
//根据cookie名获取cookie
if myCookie, err := req.Cookie("myCookie"); err == nil {
fmt.Println(myCookie.Name, ":", myCookie.Value)
}
}