-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: spec: remove string(int) #3939
Comments
I've been beaten by this "feature" in production code today. type XType int16
const x XType = 0x0001
var y = string(x) I think that |
It is valid code, so vet will not complain about it. Perhaps golint should, but not vet. |
Would it be possible to make string(int), return the string representation of the int. More users will probably expect this behaviour:
EDIT: got it, bad idea |
@urandom I would not expect an innocuously looking conversion to invoke an expensive number formatting routine. The solution to fixing a (supposedly) confusing construct isn't to replace it with another, equally confusing one. |
No, that is not possible. It will silently break existing programs in a subtle way, for no real reason. If you want to convert an int (or a float or a bool) to a string, use package strconv. In particular, using strconv allows you more control over the exact formatting. |
I am beginning to wonder whether removing the I see from #32479 that the intermediate step of adding a check for this to Although one wouldn't expect this to occur in practice, people will no doubt have used these conversions in weird, unexpected but (to them) reasonable ways knowing precisely what the results would be. Incidentally, just to reinforce the point that the func newToken(tokenType token.TokenType, ch byte) token.Token {
return token.Token{Type: tokenType, Literal: string(ch)}
} The intended audience for this book is people who know enough Go to be able to make sense of the code and I don't think those people are going to be impressed if the code in the book fails to compile (or vet). |
It's clearly essential to find how much working code that is correctly using the conversion will break. We don't know that yet. |
Issue golang/go#3939 proposes to remove string(int) from the language on the grounds that it produces non-obvious results that can't be statically checked. An intermediate step is to have go vet check for these sorts of conversions. This change adds an analysis pass to check for string(int) conversions. It suggests a fix to replace string(int) with string(rune(int)). Updates golang/go#32479. Change-Id: Ifafd6d74f9bd4a903ce6b29ac3a3c7a15f8a1ad9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/212919 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
The upcoming change in Go 1.15 `go vet` command disallows type casts from `int` to `string`. This PR replaces `string(int)` conversions with explicit formatting. golang/go#3939
In 1.15 we have a vet warning for this case (#32479). Putting this issue on hold until we have more experience with that. Also waiting until we can assume that everyone is using Go modules with a language version. |
Make test will fail due to Go1.15 starts to not allow "string(int)" expression. Fix affected sources by replace such expression to legal expressions. More info about ban string(int), see: golang/go#3939
Make test will fail due to Go1.15 starts to not allow "string(int)" expression. Fix affected sources by replace such expression to legal expressions. More info about ban string(int), see: golang/go#3939 Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
Make test will fail due to Go1.15 starts to not allow "string(int)" expression. Fix affected sources by replace such expression to legal expressions. More info about ban string(int), see: golang/go#3939 Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
) Make test will fail due to Go1.15 starts to not allow "string(int)" expression. Fix affected sources by replace such expression to legal expressions. More info about ban string(int), see: golang/go#3939 Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
Make test will fail due to Go1.15 starts to not allow "string(int)" expression. Fix affected sources by replace such expression to legal expressions. More info about ban string(int), see: golang/go#3939 Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
The text was updated successfully, but these errors were encountered: