Skip to content

Commit cf7dd73

Browse files
authored
context: fix context key (#46)
1 parent d6b9de0 commit cf7dd73

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

context.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import (
99
"go.uber.org/zap"
1010
)
1111

12-
var (
12+
type (
1313
ctxLogger struct{}
1414
ctxClient struct{}
1515
)
1616

1717
// WithLogger returns the context with zap.Logger value.
1818
func WithLogger(ctx context.Context, logger *zap.Logger) context.Context {
19-
return context.WithValue(ctx, ctxLogger, logger)
19+
return context.WithValue(ctx, ctxLogger{}, logger)
2020
}
2121

2222
// LoggerFromContext extracts zap.Logger from context.
2323
func LoggerFromContext(ctx context.Context) *zap.Logger {
24-
logger, ok := ctx.Value(ctxLogger).(*zap.Logger)
24+
logger, ok := ctx.Value(ctxLogger{}).(*zap.Logger)
2525
if !ok {
2626
return zap.NewNop()
2727
}
@@ -31,5 +31,5 @@ func LoggerFromContext(ctx context.Context) *zap.Logger {
3131

3232
// WithClient returns the context with Client value.
3333
func WithClient(ctx context.Context, client Client) context.Context {
34-
return context.WithValue(ctx, ctxClient, client)
34+
return context.WithValue(ctx, ctxClient{}, client)
3535
}

0 commit comments

Comments
 (0)