示例代码:

package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	router.GET("/cookie", func(c *gin.Context) {
		cookie, err := c.Cookie("gin_cookie")

		if err != nil {
			cookie = "NotSet"
			c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", http.SameSiteDefaultMode, false, true)
		}

		fmt.Printf("Cookie value: %s \n", cookie)
	})

	router.Run()
}

c.SetCookie 的第6个参数是设置 Cookie 的 SameSite 属性,可以用以下常量表示:http.SameSiteDefaultModehttp.SameSiteLaxModehttp.SameSiteStrictModehttp.SameSiteNoneMode

关于 SamSite 的资料请阅读:http://www.ruanyifeng.com/blog/2019/09/cookie-samesite.html