Pointers and Maps in Golang
In Go, maps are reference types, meaning they store the reference of the actual data instead of the value itself. This allows for efficient memory management, as multiple objects can point to the same underlying data.
However, trying to obtain a pointer to a map using the & operator in Go may result in an error. This is because maps in Go are already passed by reference. When you perform an assignment, such as valueTo := &valueToSomeType, you are not creating a pointer but assigning the reference value of the map to a new variable. This effectively points to the same underlying data as the original map.
Therefore, the expression valueTo[number] will not work on a pointer to a map. Instead, you should access the map elements directly using the . operator, as valueTo[number].
In summary, maps in Go are already reference types, so there is no need to use pointers to achieve reference semantics. Accessing map elements can be done directly using the . operator, as the map is always passed by reference.
The above is the detailed content of Why Can't I Get a Pointer to a Map in Go?. For more information, please follow other related articles on the PHP Chinese website!