Is it safe to remove selected keys from Golang map within a range loop? -


how can 1 remove selected keys golang map? safe combine delete() range, in code below?

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

package main  import "fmt"  type info struct {     value string }  func main() {     table := make(map[string]*info)      := 0; < 10; i++ {         str := fmt.sprintf("%v", i)         table[str] = &info{str}     }      key, value := range table {         fmt.printf("deleting %v=>%v\n", key, value.value)         delete(table, key)     } } 

this safe! can find similar sample in effective go:

for key := range m {     if key.expired() {         delete(m, key)     } } 

and the language specification:

the iteration order on maps not specified , not guaranteed same 1 iteration next. if map entries have not yet been reached removed during iteration, corresponding iteration values not produced. if map entries created during iteration, entry may produced during iteration or may skipped. choice may vary each entry created , 1 iteration next. if map nil, number of iterations 0.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -