Skip to content

Commit c5f064e

Browse files
mdlayherbradfitz
authored andcommitted
context: discourage use of basic types as keys in WithValue
Fixes #17302 Change-Id: I375d5d4f2714ff415542f4fe56a548e53c5e8ba6 Reviewed-on: https://go-review.googlesource.com/30134 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent ca04091 commit c5f064e

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/context/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
443443
// Use context Values only for request-scoped data that transits processes and
444444
// APIs, not for passing optional parameters to functions.
445445
//
446-
// The provided key must be comparable.
446+
// The provided key must be comparable and should not be
447+
// of type string or any other built-in type.
448+
// Users of WithValue should define their own types for keys.
447449
func WithValue(parent Context, key, val interface{}) Context {
448450
if key == nil {
449451
panic("nil key")

0 commit comments

Comments
 (0)