0

0

How to Properly Remove a Cookie in Go

聖光之護

聖光之護

发布时间:2026-01-12 13:23:55

|

446人浏览过

|

来源于php中文网

原创

How to Properly Remove a Cookie in Go

in go, deleting a cookie requires explicitly setting its maxage to -1 and matching the original path (and optionally domain, secure, or httponly)—simply modifying a cookie retrieved from the request and re-sending it will not work due to missing or mismatched attributes.

To reliably delete a cookie in Go, you must overwrite it with a new cookie that has the same name, path, domain (if applicable), and an expiration in the past. Browsers ignore cookies with mismatched scope attributes—even if the name matches—so reusing the cookie object from r.Cookie() is error-prone: it may lack Path, Domain, or Secure flags set during initial creation, causing the deletion attempt to be ignored.

Here’s the correct approach:

http.SetCookie(w, &http.Cookie{
    Name:     "login",
    Value:    "",                      // optional, but conventional
    Path:     "/app",                  // must match original cookie's Path
    Domain:   "example.com",           // include only if originally set
    MaxAge:   -1,                      // signals immediate expiration
    Expires:  time.Now().Add(-100 * time.Hour), // fallback for older IE
    Secure:   true,                    // must match original (if HTTPS-only)
    HttpOnly: true,                    // must match original (if used)
})

⚠️ Key points:

厉害猫AI
厉害猫AI

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

下载
  • Path is critical: If the original cookie was set with Path: "/admin", omitting or mis-setting Path here will leave the cookie intact.
  • Domain matters: If the cookie was set with Domain: ".example.com", you must include the exact same value (note the leading dot).
  • Secure and HttpOnly must match: These are part of the cookie’s identity; mismatch prevents overwrite.
  • Avoid reusing r.Cookie(): It returns only what the browser sent—not the full original cookie spec. Always reconstruct the deletion cookie manually using known creation parameters.

? Pro tip: When setting cookies initially, store their configuration (e.g., path, domain, flags) in constants or config structs—this ensures consistency when deleting later.

In summary: cookie deletion in Go is not “removal” but intentional overwriting with an expired, scoped counterpart. Precision in attributes—not just name—is what makes it work.

相关专题

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

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

731

2023.08.22

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

scripterror怎么解决
scripterror怎么解决

scripterror的解决办法有检查语法、文件路径、检查网络连接、浏览器兼容性、使用try-catch语句、使用开发者工具进行调试、更新浏览器和JavaScript库或寻求专业帮助等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

187

2023.10.18

500error怎么解决
500error怎么解决

500error的解决办法有检查服务器日志、检查代码、检查服务器配置、更新软件版本、重新启动服务、调试代码和寻求帮助等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

271

2023.10.25

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

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

269

2023.11.13

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号