网页
综合
更新时间 2026-11-14 09:29:40
spec: remove string(int) · Issue #3939 · golang/go · GitHub
自动蜘蛛
go.dev
正文约 2357 字
Labels changed: added go2.
Labels changed: added repo-main.
Labels changed: added release-none.
go/types, types2: implement new string(x) conversion restrictions
Out of curiosity: is this not the first breaking change (as in "code that would compile with Go 1.27 may no longer compiler with Go 1.28") ever introduced by a new version of Go (post 1.0)?
@jub0bs In a strict sense that is partly true (it's not the first, because we changed the semantics of loop iteration variables before). But go vet has checked exactly this for many years now, and the carve-outs in this proposal allow for exactly the cases where string(integer) still matters (like string(os.PathSeparator)). This has all been discussed in this issue.
I also note that the proposed new restriction only applies to Go 1.28 and newer. Thus, in the unlikely case that old code doesn't have a Go version and makes use of, say string(65), it will either need to be fixed (string(rune(65))) or have a Go module version. The latter is typically trivial; the former may be trivial, too.
No change in consensus, so accepted. 🎉 This issue now tracks the work of implementing the proposal. — aclements for the proposal review group
The proposal is to restrict integer-to-string conversions to only byte or rune, starting in language version 1.28.
This restriction has long been implemented as a vet check. This proposal simply lifts it to the language level.
The detailed spec wording can be found in https://go.dev/cl/830044. The core change is simple (but could use some word-smithing):
spec: string(x) for integers requires x to be of (underlying) type by…
How will named types with byte/uint8 or rune/int32 as underlying type work?
Will string(myByte('A')) work, or will you have to do string(byte(myByte('A')))
@rothelius Conversions from a named uint8 or int32 type to string should continue to work as they do today.
This is clear in the spec change https://go.dev/cl/830044.
@rothelius Conversions from a named uint8 or int32 type to string should continue to work as they do today.
This is clear in the spec change https://go.dev/cl/830044.
Ah, I see it's been amended. Yes, much clearer now 😃
Change https://go.dev/cl/862383 mentions this issue: doc: document new string conversion restrictions (update release notes)
doc: document new string conversion restrictions (update release notes)
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。