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

panic: runtime error: index out of range in for statement with an uint64 typed i #17940

Closed
hyper0x opened this issue Nov 16, 2016 · 5 comments
Closed

Comments

@hyper0x
Copy link

hyper0x commented Nov 16, 2016

Please answer these questions before submitting your issue. Thanks!

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

go version go1.7.3 darwin/amd64

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

GOARCH="amd64"
GOBIN="/Users/haolin/Gobin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/haolin/Projects/gopcp.v2"
GORACE=""
GOROOT="/usr/local/go1.7"
GOTOOLDIR="/usr/local/go1.7/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ts/7lg_tl_x2gd_k1lm5g_48c7w0000gn/T/go-build893809424=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

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/8EjmBiJS1h

What did you expect to see?

It run without any error.

What did you see instead?

panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x90ea0, 0xc42000a0f0)
/usr/local/go1.7/src/runtime/panic.go:500 +0x1a1
main.main()
/Users/haolin/demo.go:10 +0x1c1
exit status 2

Update: That's my fault. I am sorry to interrupt. My brain was imprisoned. ;-)

@hyper0x hyper0x changed the title panic: runtime error: index out of range in for statement with a uint64 typed i panic: runtime error: index out of range in for statement with an uint64 typed i Nov 16, 2016
@0xmohit
Copy link
Contributor

0xmohit commented Nov 16, 2016

The following is broken:

for i := size - 1; i >= 0; i-- {

(Note that you declared size to be an unsigned integer.)

@sathishid
Copy link

Replace the for loop with the below code to fix this usage issue.

for i := int(size) - 1; i >= 0; i--

@quentinmit
Copy link
Contributor

This is not a bug.

each variable is given the type of the corresponding initialization value in the assignment

https://tip.golang.org/ref/spec#Variable_declarations

There is nothing special about assignments found in for statements. Your expression has a type because size has a type.

Except for shift operations, if one operand is an untyped constant and the other operand is not, the constant is converted to the type of the other operand.

@ianlancetaylor
Copy link
Contributor

@g0hacker Let's take discussion about the language to a forum, not the issue tracker. See https://golang.org/wiki/Questions . Thanks.

@hyper0x
Copy link
Author

hyper0x commented Nov 17, 2016

@golang golang locked and limited conversation to collaborators Nov 17, 2017
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

6 participants