Setting cookies with net/http

前端 未结 7 1455
说谎
说谎 2020-12-12 23:12

I\'m trying to set cookies with Go\'s net/http package. I have:

package main

import \"io\"
import \"net/http\"
import \"time\"

func indexHandler(w http.Res         


        
7条回答
  •  攒了一身酷
    2020-12-12 23:52

    First, you need to create Cookie and then using http package's SetCookie() function you can set the cookie.

    expire := time.Now().Add(10 * time.Minute) 
    cookie := http.Cookie{Name: "User", Value: "John", Path: "/", Expires: expire, MaxAge: 90000}
    http.SetCookie(w, &cookie)
    

提交回复
热议问题