Skip to content

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

Closed
@TBWISK

Description

@TBWISK

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/go@1.9/1.9.5/libexec"
GOTOOLDIR="/usr/local/Cellar/go@1.9/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

Activity

dhowden

dhowden commented on May 9, 2018

@dhowden
Contributor

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

agnivade commented on May 9, 2018

@agnivade
Contributor

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.

locked and limited conversation to collaborators on May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dhowden@agnivade@TBWISK@gopherbot

        Issue actions

          error with range can't traverse "\xd5\xbc\xbd\xbe" · Issue #25306 · golang/go