讲师中心 微信公众号
AI工具推荐 视频效率加速

How to Properly Delete a Cookie in Go

夏萱大大_4049

夏萱大大_4049

发布时间: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:

  • ✅ 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.

热门AI工具

更多
Seko
Seko Hot

一款AI视频创作工具,主要用于商汤科技推出的创编一体的AI短视频创作Agent,适合需要提升相关任务效率的用户。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

UP简历
UP简历 Hot

一款AI办公效率工具,主要用于基于AI技术的免费在线简历制作工具,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

超级简历WonderCV

一款AI办公效率工具,主要用于免费求职简历模版下载制作,应届生职场人必备简历制作神器,适合需要提升相关任务效率的用户。

SkildArt
SkildArt Hot

SkildArt是一款AI文本写作工具,一站式 AI 视觉创作平台。

UpDream
UpDream Hot

一款AI视频创作工具,主要用于哔哩哔哩推出的自研AI视频创作工具,适合需要提升相关任务效率的用户。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

讯飞绘文

讯飞绘文是一款由科大讯飞推出的一站式 AIGC 内容运营平台。

相关专题

更多
while的用法
while的用法

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

334

2023.09.25

cookie
cookie

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

10979

2023.06.30

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

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

946

2023.11.23

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

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

724

2024.02.23

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

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

1826

2025.08.19

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

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

2491

2023.11.13

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

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

785

2023.12.29

Buffalo框架数据库开发全教程
Buffalo框架数据库开发全教程

本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。

120

2026.09.23

Buffalo框架路由与请求处理实操指南
Buffalo框架路由与请求处理实操指南

本专题讲解Buffalo框架路由与请求处理机制,涵盖路由注册与分组、资源路由、Handler编写规范、Context上下文方法、参数绑定、中间件编写挂载、Session与Cookie读写、Flash消息及错误页面定制方法。

40

2026.09.23

热门下载

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

精品课程

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

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn