Re: [go-nuts] Beginner Question : Reassignment of counter variable inside for loop in https://github.com/golang/mobile/tree/master/example/flappy
Ah Thanks. Sorry i missed it in the FAQ. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Beginner Question : Reassignment of counter variable inside for loop in https://github.com/golang/mobile/tree/master/example/flappy
On Thu, Feb 23, 2017 at 8:59 AM, Victor Kovacswrote: > > // The ground. >> for i := range g.groundY { >> i := i >> // The top of the ground. >> newNode(func(eng sprite.Engine, n *sprite.Node, t clock.Time) { >> > > While this solves the issue I would like to understand what is happening. > Any insight is appreciated. > > This is a variant of https://golang.org/doc/faq#closures_and_goroutines Your function literal captures a reference to the variables 'i' and 'j', These variables are modified by the loop on the next iteration. The reason for using i := i is to copy the value from the loop variable 'i' in to a new variable also called 'i', which won't be modified by the next iteration of the loop. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Beginner Question : Reassignment of counter variable inside for loop in https://github.com/golang/mobile/tree/master/example/flappy
Hi, I've recently started learning go as a way to learn mobile apps and have started experimenting with flappy from the mobile examples (https://github.com/golang/mobile/tree/master/example/flappy) While doing some changes I've run into a problem that I don't understand. The code : for i :=0; i <2; i++{ > for j :=0; j<2 ; j++{ > newNode(func(eng sprite.Engine, n *sprite.Node, t clock.Time) { > fmt.Println(i, j) produces this error with i and j being 2 : panic: runtime error: index out of range > The example code https://github.com/golang/mobile/tree/master/example/flappy solves this by assigning i again inside the loop-block : starting ligne 96 : > > newNode := func(fn arrangerFunc) { > n := {Arranger: arrangerFunc(fn)} > eng.Register(n) > scene.AppendChild(n) > } > > // The ground. > for i := range g.groundY { > i := i > // The top of the ground. > newNode(func(eng sprite.Engine, n *sprite.Node, t clock.Time) { > While this solves the issue I would like to understand what is happening. Any insight is appreciated. Thanks for your help, Victor -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.