Something I’ve noticed as a Go novice that I’d like an explanation on:
Let’s say I have a map that I pass by reference to a function
myMap := make(map[string]int)
myFunc(&myMap)
Inside of the func, I have to copy that map to a local variable in the function, modify that, then re-copy it before the map that I want to modify is actually modified:
func myFunc(tMap *map[string]int) {
tmpMap := *tMap
tmpMap["Goose"] = 11
tmpMap["Gander"] = 11
*tMap = tmpMap
}
This wouldn’t work if I tried to directly modify the map that I passed in. For instance the below:
func myBrokeFunc(tMap *map[string]int) {
tMap["Goose"] = 14
*tMap["Gander"] = 14
}
causes my IDE to complain in both instances that I can’t index a pointer to a map.
What is the cause of this? I understand that there could be issues with concurrent writes, but that would be the case with other variables that I can do this with. This is a very interesting problem to me, and I’d love to know what’s going on under the hood.