如何检查cookie是否过期

How to check if a cookie is expired

如何检查通过 GET 请求获得的 cookie 是否已过期? 我试过了:

cookie, err := c.Request.Cookie("session")
if err == nil && time.Now().Before(cookie.Expires) {
    log.Printf("COOKIE IS STILL GOOD. YUM!\n")
    return
}

但是当我调试时,我发现到期日期不正确:

2019/05/15 01:23:46 0001-01-01 00:00:00 +0000 UTC

日期是今天,而不是我设置的 2051 年,其他都没有意义。发生了什么事?

因为客户端仅在 Cookie header, the Name and Value fields are the only fields set in this context. The Expires field has the time.Time zero value 中发送名称和值,如日志输出所示。

应用程序必须在 cookie 协议之外做一些事情来确定 cookie 是否仍然有效。