Pass the map by value.
Pass the map by value.
Dave.
The only reference that comes to mind right now is:
http://golang.org/doc/effective_go.html#maps
"Like slices, maps are a reference type"
Meaning that when passed to a function, only the reference it holds is
passed.
I'm a newcomer to Go, having used the language since this pastOn Thu, Apr 26, 2012 at 11:04 AM, Miguel Pignatelli <eme...@gmail.com> wrote:
> On 26/04/12 14:11, Lars Pensjö wrote:
>>
>> A more correct statement could be "A slice holds a reference to the
>> underlying array", instead of "A slice is a reference type"?
>
>
> I agree that the documentation can be a bit clearer about this.
> Also, I think that a blog post like [1] on map internals would be very
> welcome (maybe now that Go1 is here).
>
> [1] http://research.swtch.com/godata
Saturday. Over these few days I've gone through all available
documentation on golang.org and most of the articles on Russ's site.
Personally, I think that this information should be included in the
official language documentation, not just a blog post. Russ's
descriptions of the data type internals (above), how interfaces are
implemented [1], and even the details of the language at the assembly
level [2], have been extremely helpful to me in building up an overall
picture of the ideas behind the language.
I'm not suggesting that any of this information be included in the
introduction for new users, but it should be there in an appendix
somewhere for people who are interested in this level of detail.
I would also suggest documenting things such as performance
characteristics of various data structures. After reading the official
documentation I still had no idea whether a map is implemented as a
hash table or as a red-black tree, for example. Sometimes it's very
helpful to know these things. I finally found the answer by skimming
through the source code.
On Thursday, 26 April 2012 12:50:41 UTC+2, emepyc wrote:The only reference that comes to mind right now is:
http://golang.org/doc/effective_go.html#maps
"Like slices, maps are a reference type"
I find the statement "a slice is a reference type" slightly misleading. A slice consists of two parts, a background array that is indeed referenced, and then a pointer and length information. The second part of this is just a value. If you pass a slice by value, only the underlying array can be changed.A map, on the other hand, is a true reference type I think. If you pass it by value, you can still add elements to it.
What I find odd, however, is that in order to append to a slice, I need to pass by reference (otherwise the caller of the function might not see the changes), but in order to add to a map, I need to pass by value. If both slices and maps were truly reference types (as suggested by the following phrase of the doc: Slices, maps and channels are reference types), then this distinction would not be necessary.