From bd7249766617fda12d112c3ad3ae2857ff97c71e Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 10 Apr 2016 15:45:34 +0000 Subject: context: document that WithValue's key must be comparable Also, check it and explode earlier, rather than cryptic failures later. Change-Id: I319a425f60e2bc9d005a187fbdbd153faa96411c Reviewed-on: https://go-review.googlesource.com/21799 Reviewed-by: Andrew Gerrand Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Minux Ma --- src/context/context.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/context/context.go') diff --git a/src/context/context.go b/src/context/context.go index 21dc8676bf..c332e1f443 100644 --- a/src/context/context.go +++ b/src/context/context.go @@ -39,6 +39,7 @@ package context import ( "errors" "fmt" + "reflect" "sync" "time" ) @@ -424,7 +425,12 @@ func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { // // Use context Values only for request-scoped data that transits processes and // APIs, not for passing optional parameters to functions. -func WithValue(parent Context, key interface{}, val interface{}) Context { +// +// The provided key must be comparable. +func WithValue(parent Context, key, val interface{}) Context { + if !reflect.TypeOf(key).Comparable() { + panic("key is not comparable") + } return &valueCtx{parent, key, val} } -- cgit v1.3 From 87bca88c703c1f14fe8473dc2f07dc521cf2b989 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 26 Apr 2016 18:54:12 -0700 Subject: context: fix doc typo Fixes #15449 Change-Id: I8d84d076a05c56694b48f7b84f572b1a6524f522 Reviewed-on: https://go-review.googlesource.com/22493 Reviewed-by: Andrew Gerrand --- src/context/context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/context/context.go') diff --git a/src/context/context.go b/src/context/context.go index c332e1f443..da294b1292 100644 --- a/src/context/context.go +++ b/src/context/context.go @@ -67,7 +67,7 @@ type Context interface { // // // Stream generates values with DoSomething and sends them to out // // until DoSomething returns an error or ctx.Done is closed. - // func Stream(ctx context.Context, out <-chan Value) error { + // func Stream(ctx context.Context, out chan<- Value) error { // for { // v, err := DoSomething(ctx) // if err != nil { -- cgit v1.3