Programming/golang2022. 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]

'Programming > golang' 카테고리의 다른 글

go 루틴  (0) 2022.09.06
golang https server  (0) 2022.09.05
golang http redirect  (0) 2022.09.02
golang html form post 처리하기  (0) 2022.09.02
golang http.HandleFunc(pattern)  (0) 2022.08.31
Posted by 구차니