On Sun, Jan 6, 2019 at 10:38 PM <bucha...@gmail.com> wrote:
>
> https://play.golang.org/p/NnACN5fLPT3
>
> I was taking the address of the for-loop variable ("&v" in the example above) 
> and was surprised to find that all my items pointed the same address. I 
> thought I had found most of the Golang gotchas by now, but this is a new one 
> to me.

This is related to https://golang.org/doc/faq#closures_and_goroutines
.  But in general in a range loop the value is copied from the range
source to the loop variable.  Your sample code seems to think that the
loop variable is a reference to the slice element, but it's not; it's
a copy.  It's not surprising that the address of that copy, the loop
variable v, is always the same.

Ian

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