Programming/golang

golang 쿠키

구차니 2022. 9. 2. 16:44

 

func createCookie(w http.ResponseWriter, req *http.Request) { 
    http.SetCookie(w, &http.Cookie{
        Name: "name of cookie",
        Value: "value of cookie",
        Path: "/",
    })
}

func expireCookie(w http.ResponseWriter, req *http.Request) {
    cookie, err := r.Cookie("cookie-name")
    
    if err := nil{
    }
    // 1. 쿠키 삭제
    // cookie.MaxAge = -1
    
    // 2. 쿠키를 5초간 지속
    // cookie.MaxAge = 5
    
    // 3. expires 설정, 현재시간으로 부터 1시간 뒤 == 쿠키의 만료시각
    // expiration := time.Now().Add(time.Hour)
    // cookie.Expires = expiration
    
    http.SetCookie(w, cookie)
    http.Redirect(w, req, "/", http.StatusSeeOther)
}

[링크 : https://velog.io/@j1mmyson/golang-go언어에서의-쿠키-사용]

[링크 : https://pkg.go.dev/net/http#Request.Cookie]

[링크 : https://pkg.go.dev/net/http#SetCookie]