Certain types in Go are reference types: maps, slices, channels, functions, and methods.
Sometimes you need to use pointers to references. For example,
type Stack []interface{}
func (stack *Stack) Push(x interface{}) {
*stack = append(*stack, x)
}
You need it because all arguments are passed by copying the value, and append()
might need to reallocate memory in the slice's capacity is not big enough. I get that.
First question. How about map
types? If I have a custom type based on a map
, should I better always pass a pointer to it if some key:value insertions or deletions are expected?
Second question. What about other reference types? Channel
, for example. I can imagine a situation where I build a custom type based on a channel to implement some custom pre-processing for the values being passed to a channel. Pointers needed here too?
Sorry if this is basic as heck, but I really want to get a good grasp of the subject.
Reference types in Go are the set of slice, map, channel, interface, and function types. When you declare a variable from one of these types, the value that's created is called a header value.
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.
String is a reference type, not a value type.
Go slice is reference type A slice is a reference type in Go. This means that when we assign a reference to a new variable or pass a slice to a function, the reference to the slice is copied.
The rules are fairly easy when you think of everything as a value, where some values contain pointers internally.
There are of course exceptions, like if you want to be able to swap out a map entirely you would need to use pointer to do so, but these are rare cases.
There isn't really a dichotomy between "value types" and "reference types". "Reference type" is just used to describe a value type whose "value" consists wholly of a single pointer.
This is true for map and channel types, which are basically pointer types to an internal structure. But this is not completely true for slices, because a slice is a composite type (basically a struct), consisting of two integer values (length & capacity) and a pointer (to the elements). So it is a "reference type" with respect to the elements, which are accessed through the pointer, but it is a "value type" with respect to the length and capacity.
Appending to a slice operates on its length and potentially capacity, so it needs to change the "value" of the slice, whereas assigning to elements in place just uses the pointer, and thus does not need to change the "value" of the slice. You might also need to change the "value" of a slice if you want it to change the pointer to point to the same as another slice (which you would do by assigning to the slice).
It's similar for the "reference types", maps and channels. Changing the "contents" of the map or channel (which is in the stuff pointed to by the pointer) doesn't require changing the "value" of the map or channel. But if you wanted to change the pointer to point to a different underlying map or channel, then you would change the "value" of the map or channel variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With