diff options
author | Robert Griesemer <gri@golang.org> | 2023-06-12 14:58:32 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2023-06-13 17:41:11 +0000 |
commit | 17dcbd866241c69990c23ce543ebd2906d546961 (patch) | |
tree | 73d863fa9a9b7883f8c07f156198ecef54e9a105 /doc/go_spec.html | |
parent | ee5af6103da43c44104b1d06eaf5a23cd9b87085 (diff) | |
download | go-17dcbd866241c69990c23ce543ebd2906d546961.tar.gz go-17dcbd866241c69990c23ce543ebd2906d546961.zip |
spec: de-emphasize string(int) conversions
Fixes #60731.
Change-Id: I71fad1c8385b13d036bb0ce7ae6bd21e0f596e51
Reviewed-on: https://go-review.googlesource.com/c/go/+/502657
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Bypass: Robert Griesemer <gri@google.com>
Diffstat (limited to 'doc/go_spec.html')
-rw-r--r-- | doc/go_spec.html | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/doc/go_spec.html b/doc/go_spec.html index bb5b2f3db9..6e735e4373 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -5588,21 +5588,6 @@ succeeds but the result value is implementation-dependent. <ol> <li> -Converting a signed or unsigned integer value to a string type yields a -string containing the UTF-8 representation of the integer. Values outside -the range of valid Unicode code points are converted to <code>"\uFFFD"</code>. - -<pre> -string('a') // "a" -string(-1) // "\ufffd" == "\xef\xbf\xbd" -string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8" - -type myString string -myString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5" -</pre> -</li> - -<li> Converting a slice of bytes to a string type yields a string whose successive bytes are the elements of the slice. @@ -5668,6 +5653,31 @@ runes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4} []myRune(myString("🌐")) // []myRune{0x1f310} </pre> </li> + +<li> +Finally, for historical reasons, an integer value may be converted to a string type. +This form of conversion yields a string containing the (possibly multi-byte) UTF-8 +representation of the Unicode code point with the given integer value. +Values outside the range of valid Unicode code points are converted to <code>"\uFFFD"</code>. + +<pre> +string('a') // "a" +string(65) // "A" +string('\xf8') // "\u00f8" == "ø" == "\xc3\xb8" +string(-1) // "\ufffd" == "\xef\xbf\xbd" + +type myString string +myString('\u65e5') // "\u65e5" == "日" == "\xe6\x97\xa5" +</pre> + +Note: This form of conversion may eventually be removed from the language. +The <a href="/pkg/cmd/vet"><code>go vet</code></a> tool flags certain +integer-to-string conversions as potential errors. +Library functions such as +<a href="/pkg/unicode/utf8#AppendRune"><code>utf8.AppendRune</code></a> or +<a href="/pkg/unicode/utf8#EncodeRune"><code>utf8.EncodeRune</code></a> +should be used instead. +</li> </ol> <h4 id="Conversions_from_slice_to_array_or_array_pointer">Conversions from slice to array or array pointer</h4> |