Re: [go-nuts] why can't change map when in for range statements?

2018-01-16 Thread Marvin Renich
* sheepbao  [180115 21:24]:
> I wrote this code, and why `map=nil` don't stop the loop?
> 
> func mapTest() {
> for k, v := range m {
> println(k, v)
> m = nil
> }
> }
> 
> output:
> 
> 1 1
> 
> 2 2
> 
> I don't understand when I set  `m = nil`, the loop is not stop. m doesn't 
> seem to be affected.

While Go does not have any "reference" types, maps have something
similar to reference semantics.  What is stored in a map variable can be
thought of as a pointer to the map data (I believe it is really several
pointers in a struct, but we don't need that level of detail for this
explanation).  The value of variable m is evaluated once, giving a value
that represents a map.  Changing the variable m in the loop (by
assigning it a completely new value) does not change the map being used
by the loop.  This is what «m = nil» is doing; m no longer refers to the
same map data being held by the loop.

However, using the original variable m to change the map value (because
m and the value held by the loop refer to the same data) can change the
keys and values which are seen by the loop.  «delete(m, 2)» modifies the
same map data that the loop is using.

Note at https://golang.org/ref/spec#For_statements under "For statements
with range clause" it says:

  3. The iteration order over maps is not specified and is not
 guaranteed to be the same from one iteration to the next. If a map
 entry that has not yet been reached is removed during iteration,
 the corresponding iteration value will not be produced. If a map
 entry is created during iteration, that entry may be produced
 during the iteration or may be skipped. The choice may vary for
 each entry created and from one iteration to the next. If the map
 is nil, the number of iterations is 0. 

So, adding new map elements within the for statement may or may not
include those elements in a subsequent iteration of the for statement.

The key point is whether you are changing the contents of the variable
in the range expression or the value obtained from that variable.

...Marvin

-- 
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] why can't change map when in for range statements?

2018-01-15 Thread sheepbao
I wrote this code, and why `map=nil` don't stop the loop?
```go

func mapTest() {

m := map[int]int{1: 1, 2: 2}

for k, v := range m {

println(k, v)

m = nil

}

}

```

output:

1 1

2 2


I don't understand when I set  `m = nil`, the loop is not stop. m doesn't 
seem to be affected.


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