0

0

How to Properly Delete a Cookie in Go

聖光之護

聖光之護

发布时间:2026-01-12 13:24:10

|

407人浏览过

|

来源于php中文网

原创

How to Properly Delete a Cookie in Go

in go, deleting a cookie requires explicitly overriding it with an expired cookie—setting maxage = -1 alone isn’t sufficient; you must also match the original cookie’s path (and optionally domain, secure, and httponly) to ensure the browser replaces the correct one.

To reliably remove a cookie in Go, you must send a new cookie with the same name, same path, and same domain as the original—but with an expiration time in the past. Browsers ignore cookies with mismatched attributes (especially Path), which is why reusing r.Cookie("login") and modifying only Value and MaxAge often fails: the original cookie may have been set with a specific Path (e.g., /auth or /), and the mutated cookie won’t target it unless that path is preserved.

Here’s the correct approach:

http.SetCookie(w, &http.Cookie{
    Name:     "login",
    Value:    "",                    // optional, but recommended for clarity
    Path:     "/",                   // must match the Path used when setting the cookie
    MaxAge:   -1,                    // signals immediate expiration
    Expires:  time.Now().Add(-100 * time.Hour), // fallback for older browsers (e.g., IE)
    Secure:   false,                 // set to true if served over HTTPS
    HttpOnly: false,                 // match original HttpOnly setting
    Domain:   "",                    // set only if original cookie specified a Domain
})

⚠️ Critical Notes:

厉害猫AI
厉害猫AI

遥遥领先的AI全职业办公写作平台

下载
  • Always specify Path — if your original cookie was set with Path: "/admin", you must use Path: "/admin" here. Omitting it defaults to the current request path (e.g., /logout), which won’t delete the cookie set at /.
  • Match security flags — if the original cookie had Secure: true or HttpOnly: true, replicate those. Mismatches prevent deletion.
  • Don’t reuse r.Cookie(...) — the request cookie’s Value, Expires, and other fields are irrelevant for deletion. Only Name, Path, Domain, Secure, and HttpOnly matter—and you already know Name; the rest must be recalled from where the cookie was originally set.
  • ? Expires + MaxAge = -1 together maximize compatibility: modern browsers honor MaxAge, while older ones (like Internet Explorer) rely solely on Expires.

? Best Practice: Store cookie configuration (especially Path, Domain, Secure) as constants or config values when setting cookies, so deletion logic remains consistent and maintainable.

In summary: deletion is replacement, not mutation — send a precisely matched, expired cookie, and the browser will remove it from storage on the next request.

相关专题

更多
if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

731

2023.08.22

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

84

2023.09.25

cookie
cookie

Cookie 是一种在用户计算机上存储小型文本文件的技术,用于在用户与网站进行交互时收集和存储有关用户的信息。当用户访问一个网站时,网站会将一个包含特定信息的 Cookie 文件发送到用户的浏览器,浏览器会将该 Cookie 存储在用户的计算机上。之后,当用户再次访问该网站时,浏览器会向服务器发送 Cookie,服务器可以根据 Cookie 中的信息来识别用户、跟踪用户行为等。

6415

2023.06.30

document.cookie获取不到怎么解决
document.cookie获取不到怎么解决

document.cookie获取不到的解决办法:1、浏览器的隐私设置;2、Same-origin policy;3、HTTPOnly Cookie;4、JavaScript代码错误;5、Cookie不存在或过期等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

340

2023.11.23

阻止所有cookie什么意思
阻止所有cookie什么意思

阻止所有cookie意味着在浏览器中禁止接受和存储网站发送的cookie。阻止所有cookie可能会影响许多网站的使用体验,因为许多网站使用cookie来提供个性化服务、存储用户信息或跟踪用户行为。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

405

2024.02.23

cookie与session的区别
cookie与session的区别

本专题整合了cookie与session的区别和使用方法等相关内容,阅读专题下面的文章了解更详细的内容。

88

2025.08.19

数据库Delete用法
数据库Delete用法

数据库Delete用法:1、删除单条记录;2、删除多条记录;3、删除所有记录;4、删除特定条件的记录。更多关于数据库Delete的内容,大家可以访问下面的文章。

269

2023.11.13

drop和delete的区别
drop和delete的区别

drop和delete的区别:1、功能与用途;2、操作对象;3、可逆性;4、空间释放;5、执行速度与效率;6、与其他命令的交互;7、影响的持久性;8、语法和执行;9、触发器与约束;10、事务处理。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

208

2023.12.29

Java 项目构建与依赖管理(Maven / Gradle)
Java 项目构建与依赖管理(Maven / Gradle)

本专题系统讲解 Java 项目构建与依赖管理的完整体系,重点覆盖 Maven 与 Gradle 的核心概念、项目生命周期、依赖冲突解决、多模块项目管理、构建加速与版本发布规范。通过真实项目结构示例,帮助学习者掌握 从零搭建、维护到发布 Java 工程的标准化流程,提升在实际团队开发中的工程能力与协作效率。

8

2026.01.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go 教程
Go 教程

共32课时 | 3.6万人学习

Go语言实战之 GraphQL
Go语言实战之 GraphQL

共10课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号