Skip to content
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

cmd/vet: don't complain about int to string conversion (at least in some cases) #46597

Closed
ghost opened this issue Jun 6, 2021 · 3 comments
Closed

Comments

@ghost
Copy link

ghost commented Jun 6, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.5 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/xxx/.cache/go-build"
GOENV="/home/xxx/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/xxx/gocode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/xxx/gocode"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/home/xxx/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/home/xxx/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build907859924=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/EW1W9iTRyK9

What did you expect to see?

No errors from go vet.

What did you see instead?

conversion from int to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)

It it clear from the code I do indeed need a string of one rune, not a string of digits.

@go101
Copy link

go101 commented Jun 6, 2021

for i := rune(0); i < 8; i++ works too.
It is planned that the feature of converting non-rune and none-byte integers to strings will be removed from a future Go version.

@robpike
Copy link
Contributor

robpike commented Jun 6, 2021

In future, it will be illegal to convert an int to a string this way, as it has led to too much misunderstanding. People accustomed to other languages often expect string(3) to yield "3", but in Go the result instead is "\x03", which can be surprising. Still converting a rune to a one-rune string makes perfect sense.

The decision was therefore made to transition to making that illegal by having vet complain about conversions from integer types other than runes.

This is working as intended. If the argument to string is a rune, the problem goes away, as you observed.

@robpike robpike closed this as completed Jun 6, 2021
@timothy-king
Copy link
Contributor

@opennota I think @robpike gave a very good explanation. I think the confusion may be caused by an unclear vet message. I am happy to consider improving the messages from vet if you have a suggestion.

@golang golang locked and limited conversation to collaborators Jun 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants