From b0bc18d5bcb57b5b1645899e7fc865f31f10d6f4 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 18 May 2020 15:33:29 -0700 Subject: [PATCH] doc/go1.15: mention vet warning for string(x) For #32479 Change-Id: I974709d471021d370aa9bdc5f24b02afa8bd9b54 Reviewed-on: https://go-review.googlesource.com/c/go/+/234517 Reviewed-by: Robert Griesemer --- doc/go1.15.html | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/doc/go1.15.html b/doc/go1.15.html index af0b3ba6ac1..727175539d9 100644 --- a/doc/go1.15.html +++ b/doc/go1.15.html @@ -116,6 +116,47 @@ TODO GODEBUG=modcacheunzipinplace=1.

+

Vet

+ +

New warning for string(x)

+ +

+ 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. +

+

Runtime