我需要将用户 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。
我希望这有帮助。嵌入类型
- 1 回答
- 0 关注
- 190 浏览
添加回答
举报
0/150
提交
取消