Srivathsan,

Go Frequently Asked Questions (FAQ)
What happens with closures running as goroutines?
https://golang.org/doc/faq#closures_and_goroutines

Peter

On Sunday, February 12, 2017 at 5:51:14 AM UTC-5, 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() {
>
>             log.Println("i=", i)
>
>         }()
>
>     }
>
>
>     fmt.Print("Press <Enter> to quit...")
>
>     var input string
>
>     fmt.Scanln(&input)
>
> }
>
>
> The output I expect is 0, 1, 2, 3, and 4 in any jumbled order.  But what I 
> got was 5, 5, 5, 5 and 5!  Why is that so?
>
> Thanks in advance
>
> rgds,
> Vatsan
>

-- 
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.

Reply via email to