FAQ
Can someone explain the behavior present
here: http://play.golang.org/p/xge9imM5Mi
Is the range here just reassigning the different values of each element to
the same memory address? Is this an expected thing? Seems like a bit of an
unpleasant gotcha.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Minux at Jan 1, 2015 at 11:23 pm

    On Wed, Dec 31, 2014 at 6:24 PM, wrote:

    Can someone explain the behavior present here:
    http://play.golang.org/p/xge9imM5Mi
    Is the range here just reassigning the different values of each element to
    the same memory address?
    Yes, there is only one instance of the for range variables for each for
    range loop.
    If you want separate instances for each loop iteration, you need to assign
    it to a
    new variable like what you did in the 2nd part of the code.

    It's the same as the problem answered here:
    http://golang.org/doc/faq#closures_and_goroutines

    Is this an expected thing? Seems like a bit of an unpleasant gotcha.
    >
    It's expected.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Jan 2, 2015 at 1:01 am
    The range syntax is syntactic sugar, this is what the compiler produces
    under the hood

    for i, element := 0, (foo{}); i < len(array); i++ {
    element = array[i]
    pointeryArray[i] = &bar{&element}
    }

    http://play.golang.org/p/QX7000yXs7

    As others have mentioned, this is a gotcha that comes from the convenience
    of the range syntax.
    On Thursday, 1 January 2015 10:24:08 UTC+11, artu...@gmail.com wrote:

    Can someone explain the behavior present here:
    http://play.golang.org/p/xge9imM5Mi
    Is the range here just reassigning the different values of each element to
    the same memory address? Is this an expected thing? Seems like a bit of an
    unpleasant gotcha.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Artushin at Jan 2, 2015 at 1:15 am
    Cool, thanks for the answer guys.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 1, '15 at 5:52p
activeJan 2, '15 at 1:15a
posts4
users3
websitegolang.org

3 users in discussion

Artushin: 2 posts Dave Cheney: 1 post Minux: 1 post

People

Translate

site design / logo © 2021 Grokbase