Go: reference types as arguments

心已入冬 提交于 2021-01-22 06:31:15

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/31170368/go-reference-types-as-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!