準(zhǔn)確地說(shuō), 這個(gè)標(biāo)題是有問(wèn)題的, go gin只能給瀏覽器返回操作cookie的指令, 真正執(zhí)行cookie操作的是瀏覽器。 但廣泛地來(lái)講, 說(shuō)go gin操作cookie, 也是可以的(間接操作)
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default();
router.GET("/read_cookie", func(context *gin.Context) {
val, _ := context.Cookie("name")
context.String(200, "Cookie:%s", val)
})
router.GET("/write_cookie", func(context *gin.Context) {
context.SetCookie("name", "Shimin Li", 10, "/", "localhost", false, true)
})
router.GET("/clear_cookie", func(context *gin.Context) {
context.SetCookie("name", "Shimin Li", -1, "/", "localhost", false, true)
})
router.Run(":8080")
}
自己玩了一下, OK.
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接