Home >Backend Development >Golang >How are Maps Passed in Go: By Value or By Reference?
Passing Maps by Reference
In Go, maps are fundamentally reference types. This means that when a map is passed as an argument to a function, the actual map is passed by reference, not copied.
In your example, you attempted to create pointers to maps using the following code:
valueTo := &valueToSomeType nameTo := &nameToSomeType
However, this is unnecessary as maps are already passed by reference. You can access the maps directly using the names valueToSomeType and nameToSomeType, without creating pointers.
Accessing Map Elements
To access the elements of a map, you can use the bracket notation:
value := valueToSomeType[number]
Here, value will be the value associated with the key number in the valueToSomeType map.
Additional Notes
The error internal compiler error: var without type, init: new that you encountered earlier may have been caused by a separate issue in your code. It is not related to the way you are passing maps by reference.
The above is the detailed content of How are Maps Passed in Go: By Value or By Reference?. For more information, please follow other related articles on the PHP Chinese website!