I can offer decomposition of map to 2 slices example:
func decomposeMap(m map[string]int) ([]string, []int) {
var i uint
l := len(m)
keys, values := make([]string, l), make([]int, l)
for keys[i], values[i] = range m {
i++
}
return keys, values
}
but I am failing to write map copying:
func copyMap(m map[string]int) map[string]int {
m2 := make(map[string]int, len(m))
for id, m2[id] = range m {} // error - id is not declared
for id, m2[id] := range m {} // error with m2[id] already declared
// id should not be accessible here, it should exist only inside loop
return m2
}
I can declare id as a var, but I dont want it to be available outside for loop. How can i mix assigment and declaration, eg: for id:=, m[id]= range m {} ?
So it will declare index just inside for loop, and will be not accessible outside?