From b0bc18d5bcb57b5b1645899e7fc865f31f10d6f4 Mon Sep 17 00:00:00 2001
From: Ian Lance Taylor GODEBUG=modcacheunzipinplace=1
.
+ The vet tool now warns about conversions of the
+ form string(x)
where x
has an integer type
+ other than rune
or byte
.
+ Experience with Go has shown that many conversions of this form
+ erroneously assume that string(x)
evaluates to the
+ string representation of the integer x
.
+ It actually evaluates to a string containing the UTF-8 encoding of
+ the value of x
.
+ For example, string(9786)
does not evaluate to the
+ string "9786"
; it evaluates to the
+ string "\xe2\x98\xba"
, or "☺"
.
+
+ Code that is using string(x)
correctly can be rewritten
+ to string(rune(x))
.
+ Or, in some cases, calling utf8.EncodeRune(buf, x)
with
+ a suitable byte slice buf
may be the right solution.
+ Other code should most likely use strconv.Itoa
+ or fmt.Sprint
.
+
+ This new vet check is enabled by default when using go test
.
+
+ We are considering prohibiting the conversion in a future release of Go.
+ That is, the language would change to only
+ permit string(x)
for integer x
when the
+ type of x
is rune
or byte
.
+ Such a language change would not be backward compatible.
+ We are using this vet check as a first trial step toward changing
+ the language.
+