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

error with range can't traverse "\xd5\xbc\xbd\xbe" #25306

Closed
TBWISK opened this issue May 9, 2018 · 2 comments
Closed

error with range can't traverse "\xd5\xbc\xbd\xbe" #25306

TBWISK opened this issue May 9, 2018 · 2 comments

Comments

@TBWISK
Copy link

TBWISK commented May 9, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version:1.9.5
go version:1.10.2

Does this issue reproduce with the latest release?

yes ,it reproduce in 1.10.2 version

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

I have to two go version for use
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/tbwisk/go"
GORACE=""
GOROOT="/usr/local/Cellar/[email protected]/1.9.5/libexec"
GOTOOLDIR="/usr/local/Cellar/[email protected]/1.9.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4b/mfftzbmx3t58tvq3t0_zq3100000gp/T/go-build136633725=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/tbwisk/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/tbwisk/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.2/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4b/mfftzbmx3t58tvq3t0_zq3100000gp/T/go-build563777223=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

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

my go code

package main
import "fmt"
func main() {
	ra := "\xd5\xbc\xbd\xbe"
	for idx := range ra {
		fmt.Println(idx)
	}
}

What did you expect to see?

0
1
2
3

What did you see instead?

0
2
3

@dhowden
Copy link
Contributor

dhowden commented May 9, 2018

It looks like that string it not valid UTF8, and so iterating over the UTF8 runes of the string won't work as expected:

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

This code shows the errors more clearly: https://play.golang.org/p/2d0_9LFo1L9

@agnivade
Copy link
Contributor

agnivade commented May 9, 2018

Even if it was a valid utf-8 string, one should not expect the indices to be serial. A range loop iterates over unicode code points rather than bytes. To iterate on bytes, use the regular for loop.

See https://blog.golang.org/strings (range loops section) for more detail.

Closing since this is not a bug.

@agnivade agnivade closed this as completed May 9, 2018
@golang golang locked and limited conversation to collaborators May 9, 2019
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