From: Rick Arnold Date: Thu, 25 Feb 2016 00:25:38 +0000 (-0500) Subject: expvar: document that Get returns nil for non-existent vars X-Git-Tag: go1.7beta1~1725 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=5abd327d0069af3abc353ba8ee77dd7d1c0c3e75;p=gostls13.git expvar: document that Get returns nil for non-existent vars Also added a test to ensure the behavior. Fixes #14150 Change-Id: Ib3ee9fdae59826fa594ce1be3c49b51d740b56eb Reviewed-on: https://go-review.googlesource.com/19915 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/expvar/expvar.go b/src/expvar/expvar.go index 24c2d6b29a..1d51bc97f9 100644 --- a/src/expvar/expvar.go +++ b/src/expvar/expvar.go @@ -258,7 +258,8 @@ func Publish(name string, v Var) { sort.Strings(varKeys) } -// Get retrieves a named exported variable. +// Get retrieves a named exported variable. It returns nil if the name has +// not been registered. func Get(name string) Var { mutex.RLock() defer mutex.RUnlock() diff --git a/src/expvar/expvar_test.go b/src/expvar/expvar_test.go index 8bc633e4a9..385fea81ad 100644 --- a/src/expvar/expvar_test.go +++ b/src/expvar/expvar_test.go @@ -26,6 +26,14 @@ func RemoveAll() { varKeys = nil } +func TestNil(t *testing.T) { + RemoveAll() + val := Get("missing") + if val != nil { + t.Errorf("got %v, want nil", val) + } +} + func TestInt(t *testing.T) { RemoveAll() reqs := NewInt("requests")