Why can't I do it like this in Go?
func do(m1 map[int]string) {
var m map[int]string = make(map[int]string)
*m1 = &m;
}
I have m1 map, which means I can now it's reference? How to assign a new object to that memory pointer?
Why can't I do it like this in Go?
func do(m1 map[int]string) {
var m map[int]string = make(map[int]string)
*m1 = &m;
}
I have m1 map, which means I can now it's reference? How to assign a new object to that memory pointer?
m1 is a map, and it is passed by value to the function do. That is, where the function is called, a copy of another map variable is passed. The fact that a map is a pointer type is irrelevant here. A copy of that pointer is passed.
If you want to change the map at the call site, you have to pass it as pointer:
func do(m1 *map[int]string) {
var m map[int]string = make(map[int]string)
*m1 = m;
}