You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version)?
go version devel +b836fe6 Sat Apr 2 18:25:27 2016 -0400 linux/amd64
What operating system and processor architecture are you using (go env)?
linux/amd64
Currently, the expvar.Var interface looks like it is the well known fmt.Stringer interface; both have a single String() method in them. However, this is not the case, because expvar.Var's String() requires you to return a proper JSON object, with the correct quoting and all. This means that not all fmt.Stringer methods will do; for instance, time.Time's String() method returns an unquoted string that will cause JSON parsing failures. That means that naively writing something like:
m := expvar.NewMap("myapp")
m.Set("startTime", time.Now())
will cause mysterious downstream failures in things that attempt to consume expvar variables (because time.Time returns an unquoted string, which then winds up in the JSON for /debug/vars).
This programmer mistake could be somewhat avoided if the expvar documentation noted the JSON formatting requirement and also that String() functions in other packages that are intended to satisfy fmt.Stringer will often not work to satisfy expvar.Var.
The text was updated successfully, but these errors were encountered:
Is it worth explicitly mentioning that this is not necessarily compatible with fmt.Stringer and thus common .String() usage (either in the Var comments or in general package comments)? One can work it out from the current documentation for String(), but I'm not sure it's going to be clear or obvious to people.
(Specifically, I'm not sure I'd have realized that eg time.Time was not compatible from this documentation alone. But on the other hand, the Go documentation style tends towards terse.)
Please answer these questions before submitting your issue. Thanks!
go version
)?go version devel +b836fe6 Sat Apr 2 18:25:27 2016 -0400 linux/amd64
go env
)?linux/amd64
Currently, the expvar.Var interface looks like it is the well known fmt.Stringer interface; both have a single String() method in them. However, this is not the case, because expvar.Var's String() requires you to return a proper JSON object, with the correct quoting and all. This means that not all fmt.Stringer methods will do; for instance, time.Time's String() method returns an unquoted string that will cause JSON parsing failures. That means that naively writing something like:
will cause mysterious downstream failures in things that attempt to consume expvar variables (because time.Time returns an unquoted string, which then winds up in the JSON for /debug/vars).
This programmer mistake could be somewhat avoided if the expvar documentation noted the JSON formatting requirement and also that String() functions in other packages that are intended to satisfy fmt.Stringer will often not work to satisfy expvar.Var.
The text was updated successfully, but these errors were encountered: