Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: reference types as arguments

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.

like image 996
oopcode Avatar asked Jul 01 '15 19:07

oopcode


People also ask

What are the reference types in Golang?

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.

Is Go pass by value or reference?

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.

Is string a reference type in Golang?

String is a reference type, not a value type.

Is Golang slice a reference 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.


Video Answer


2 Answers

The rules are fairly easy when you think of everything as a value, where some values contain pointers internally.

  • slices: Use a pointer when you may need to modify the length or capacity, which changes the value of the slice.
  • maps: Don't use a pointer, since the map value doesn't change with modifications.
  • functions and methods: Don't use a pointer, the same effect is had through function values.
  • chan: Don't use a pointer.

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.

like image 94
JimB Avatar answered Sep 25 '22 00:09

JimB


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.

like image 24
newacct Avatar answered Sep 26 '22 00:09

newacct