最佳答案
我正在使用 Context
和 WithValue
将 uuid 传递给处理此 *http.request
的后续函数。这个 uuid 在授权头中被传递给一个 REST 调用,以标识一个人。验证了授权令牌,需要访问该令牌以检查调用本身是否被授权。
我用:
ctx := context.WithValue(r.Context(), string("principal_id"), *id)
但戈林特抱怨道:
should not use basic type string as key in context.WithValue
什么是最好的选择,可以用来检索这个关键,而不是一个基本类型像一个简单的字符串?