FAQ
This is a snippet that recreates a problem that I'm having at the
moment.

http://play.golang.org/p/1YFsRYuv70

Obviously the e in the map range is being reused, so each time I append
the address of e into slice I am leaving it open for being overwritten
by the next iteration of the range. This is confirmed by this snippet,
which fixes the behaviour.

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

Is this expected or should I submit an issue?

thanks
Dan

--

Search Discussions

  • Nigel Tao at Sep 18, 2012 at 1:18 am

    On 18 September 2012 11:08, Dan Kortschak wrote:
    Is this expected or should I submit an issue?
    I think it's expected, and consistent with
    http://golang.org/doc/go_faq.html#closures_and_goroutines

    --
  • Andrew Gerrand at Sep 18, 2012 at 1:22 am

    On 17 September 2012 18:08, Dan Kortschak wrote:
    This is a snippet that recreates a problem that I'm having at the
    moment.

    http://play.golang.org/p/1YFsRYuv70

    Obviously the e in the map range is being reused, so each time I append
    the address of e into slice I am leaving it open for being overwritten
    by the next iteration of the range. This is confirmed by this snippet,
    which fixes the behaviour.

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

    Is this expected or should I submit an issue?
    Nope, that's how range works.

    Andrew

    --
  • Jesse McNelis at Sep 18, 2012 at 1:44 am

    On Tue, Sep 18, 2012 at 11:21 AM, Andrew Gerrand wrote:
    Nope, that's how range works.
    It's not just how range works, it's how all loops work.
    http://play.golang.org/p/YNy79DvoC0


    --
    =====================
    http://jessta.id.au

    --
  • Dan Kortschak at Sep 18, 2012 at 2:00 am
    It's more transparent in that case, but yes I see I just wasn't sure.
    On Tue, 2012-09-18 at 11:44 +1000, Jesse McNelis wrote:
    On Tue, Sep 18, 2012 at 11:21 AM, Andrew Gerrand wrote:

    Nope, that's how range works.
    It's not just how range works, it's how all loops work.
    http://play.golang.org/p/YNy79DvoC0

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 18, '12 at 1:08a
activeSep 18, '12 at 2:00a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase