[go-nuts] gotcha: don't take the address of a for-range loop variable

2019-01-06 Thread buchanae
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.

-A

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


[go-nuts] gotcha: don't take the address of a for-range loop variable

2019-01-08 Thread K Davidson
You could also do: https://play.golang.org/p/zjNVeQNbCq5

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


Re: [go-nuts] gotcha: don't take the address of a for-range loop variable

2019-01-06 Thread Ian Lance Taylor
On Sun, Jan 6, 2019 at 10:38 PM  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.


Re: [go-nuts] gotcha: don't take the address of a for-range loop variable

2019-01-07 Thread Steven Hartland
Worth mentioning you don't need the second _ variable in range 
statements e.g. use:

    for i := range items {
   .
    }

vs:
    for i, _ := range items {
   .
    }
On 07/01/2019 02:24, 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.


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


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