On Wednesday, December 6, 2017 at 9:41:01 AM UTC-5, T L wrote:
>
> ok, I get it. But how about this:
>
> package main
>
> import "fmt"
>
> func main() {
>   i := 2
>   x := []int{3, 5, 7}
>   for _, x[i] = range x {
>       
>   }
>   fmt.Println(x) // [3 5 5]
> }
>
> I expect it is still [3 5 7]
>

Eh, looks [3 5 5] is right. :)
 

>
> On Wednesday, December 6, 2017 at 9:34:38 AM UTC-5, Ian Davis wrote:
>>
>> You can see your error if you initialise i to -1 before the loop: v[i] is 
>> evaluated before i is incremented.
>>
>>
>> On Wed, 6 Dec 2017, at 02:26 PM, T L wrote:
>>
>>
>> package main
>>
>> import "fmt"
>>
>> func main() {
>>   var i int
>>   var x = []int{3, 5, 7}
>>   var y = make([]int, 3)
>>   for i, y[i] = range x {
>>       fmt.Println(i, ":", y, ", y[", i, "] =", y[i])
>>   }
>>   fmt.Println("y =", y)
>> }
>>
>> // output:
>> 0 : [3 0 0] , y[ 0 ] = 3
>> 1 : [5 0 0] , y[ 1 ] = 0
>> 2 : [5 7 0] , y[ 2 ] = 0
>> y = [5 7 0]
>>
>>
>> --
>> 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...@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.

Reply via email to