为了账号安全,请及时绑定邮箱和手机立即绑定

Golang - 将用户 ID 存储在 http 结构中

Golang - 将用户 ID 存储在 http 结构中

Go
qq_花开花谢_0 2021-11-01 10:43:53
我需要将用户 ID 存储在 w http.ResponseWriter 或 req *http.Request 中的某个位置,以便在我的处理程序中可以访问它们。我怎样才能做到这一点?这是我需要的一个小演示:func test(w http.ResponseWriter, r *http.Request){    userID := w.UserID // or something like this}同样,这个值必须存储在这些变量中的任何一个中,以便我可以在所有 http 处理程序中访问它。非常感谢您的时间
查看完整描述

1 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

您可以将 http.ResponseWriter 嵌入到您自己的结构中并添加额外的字段


type ResponseWriter struct{

    http.ResponseWriter

    UserID int

}

现在使用您的 ResponseWriter 而不是 http.ResponseWriter。


我希望这有帮助。嵌入类型


查看完整回答
反对 回复 2021-11-01
  • 1 回答
  • 0 关注
  • 190 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信