Re: [go-nuts] Unexpected Behaviour of goroutine

2017-02-12 Thread Michael Jones
asking is good! first, it helps people who are too shy or insecure to ask. second, it is part of the adventure of learning. it has often been my experience that the people with the most and best questions end up with the best understanding and spirit. On Sun, Feb 12, 2017 at 9:36 PM, Vatsan wrot

Re: [go-nuts] Unexpected Behaviour of goroutine

2017-02-12 Thread Vatsan
Thanks to all who answered; I got explanation I needed. @"Michael Jones": yes, your solution also works - "i := i" overshadows the i defined as part of the for-loop and that was also explained in one of the Go-blogs. Anyways, as I have already acknowledged earlier, this was an RTFM moment for me.

Re: [go-nuts] Unexpected Behaviour of goroutine

2017-02-12 Thread Michael Jones
These answers are all correct. Here is another way to see the problem and address it. Not as clear to the next reader perhaps so not the recommendation, but a good example to contemplate. "Precisely what does this extra statement do?" package main import "fmt" import "log" func main() { for i :

Re: [go-nuts] Unexpected Behaviour of goroutine

2017-02-12 Thread Ayan George
Srivathsan Madhavan wrote: > Hi all, > > I still consider myself a newbie to Golang hence my question here could be > due to my fundamental misunderstanding of this language > > I have the following code: > > func main() { > > for i := 0; i < 5; i++ { > > go func() { > >

Re: [go-nuts] Unexpected Behaviour of goroutine

2017-02-12 Thread Jan Mercl
On Sun, Feb 12, 2017 at 11:51 AM Srivathsan Madhavan wrote: A race detector should complain about write to i here for i := 0; i < 5; i++ { concurrently with reading i here log.Println("i=", i) Passing i to the goroutine, instead of sharing it should fix the issue go f

[go-nuts] Unexpected Behaviour of goroutine

2017-02-12 Thread Srivathsan Madhavan
Hi all, I still consider myself a newbie to Golang hence my question here could be due to my fundamental misunderstanding of this language I have the following code: func main() { for i := 0; i < 5; i++ { go func() { log.Println("i=", i) }() } fmt.