问题
According to responses and comments in Go: reference types as arguments Go does not seem to have types that are "officially" called "reference types".
Yet, there are some types that actually hold pointers to the underlying data which allows to efficiently pass values of these types as function arguments without taking pointer to them. When such value is passed as a function argument, there is no (or very little as with slices) copying of the underlying data so that there is no (or very little) efficiency gain if a programmer takes the pointer to the type and passes this pointer as the function argument instead of the value.
Such types are slices, maps, and channels according to the accepted answer.
The official Effective Go document describes Slices as:
Slices hold references to an underlying array, and if you assign one slice to another, both refer to the same array. If a function takes a slice argument, changes it makes to the elements of the slice will be visible to the caller, analogous to passing a pointer to the underlying array.
Maps as:
Like slices, maps hold references to an underlying data structure. If you pass a map to a function that changes the contents of the map, the changes will be visible in the caller.
and Channels as:
Like maps, channels are allocated with make, and the resulting value acts as a reference to an underlying data structure.
Documentation seems to avoid using the term "reference types" with regards to these types for whatever reason but still mentions the word "reference" in each case.
What also makes these types special is that one can assign nil
to a value of this type similar to pointer types (doc):
A value
x
is assignable to a variable of typeT
("x
is assignable toT
") if one of the following conditions applies:
...
x
is the predeclared identifiernil
andT
is a pointer, function, slice, map, channel, or interface type.
In the daily programmer's practice it is important to know, clearly differentiate and clearly communicate types or data structures that when passed around as function parameters share the underlying data versus the ones that don't share (but copy) the underlying data.
Being able to refer to slices, maps, and channels as a single group in conversation with other, especially junior, programmers is a big advantage to clear communication helping to ensure data safety in programs and identify/prevent hazards connected to mutating the shared sate.
How can this group of types be called in general?
回答1:
How can this [slices, maps, functions and methods, and channels] group of types be called in general?
The question is illformed as methods are not types in Go and function types are, well just functions and do not fit into the discussion about "references". Assuming you want to talk about slices, maps, closures and buffered channels:
These are called "slices, maps, closures and buffered channels" and there probably is no need to have a name for these 4: They have not much in common.
The following four types exhibit reference semantics: slices, maps, closures and buffered channels. (Unbuffered channels don't).
- A slice is a value type which presents a view into the backing array. It has thus reference semantics in regard to the values stored in the backing array.
- A map is a map and has reference semantics in regard to the values stored in the map. (This one is the closest to a reference type.)
- Function closers are closures and are complicated and talking about them with juniors in the light of value ownership probably is not helpful).
- A buffered channel shows reference semantics in regard to buffered values.
There is no need for a complicated term (unless you work in academics and classifying, separating and taxonomizing is your living.)
来源:https://stackoverflow.com/questions/65717235/is-there-a-general-name-for-go-types-with-reference-semantics-such-as-map-sli