3 ms·
"Maps and channels are not references." But then refer to the Go team's article on maps: Map types are reference types, like pointers or slices, and so the
by pokstad 9y ago
"Maps and channels are not references."
But then refer to the Go team's article on maps:
Map types are reference types, like pointers or slices, and so the value of m above is nil; it doesn't point to an initialized map. A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; don't do that. To initialize a map, use the built in make function:
m = make(map[string]int)
The make function allocates and initializes a hash map data structure and returns a map value that points to it. The specifics of that data structure are an implementation detail of the runtime and are not specified by the language itself. In this article we will focus on the use of maps, not their implementation.
Saying maps are not passed by reference is a bit deceiving. The map's pointer location is not passed by reference.
Source: https://blog.golang.org/go-maps-in-action https://blog.golang.org/go-maps-in-action
- Rapzid 9y agoIMHO saying they are is misleading as this article correctly points out nothing is, and Rob Pike himself has pointed out the same on many occasions. They are referred to as "reference types" because they contain pointers to the underlying data structure. However the thing stored in the variable is more than just a pointer, and it is passed by value. Knowing the difference can have important ramifications; particularly in the case of a slice which has its header copied when passed to a function.