fix context keys

This commit is contained in:
Caleb Doxsey 2022-07-26 13:34:43 -06:00
parent 7adda15c75
commit e6fe3b0ceb
3 changed files with 16 additions and 9 deletions

View file

@ -11,16 +11,16 @@ import (
const headerName = "x-request-id"
var contextKey struct{}
type contextKey struct{}
// WithValue returns a new context from the parent context with a request id value set.
func WithValue(parent context.Context, requestID string) context.Context {
return context.WithValue(parent, contextKey, requestID)
return context.WithValue(parent, contextKey{}, requestID)
}
// FromContext gets the request id from a context.
func FromContext(ctx context.Context) string {
if id, ok := ctx.Value(contextKey).(string); ok {
if id, ok := ctx.Value(contextKey{}).(string); ok {
return id
}
return ""