go语言接受者的选取

Posted charlieroro

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go语言接受者的选取相关的知识,希望对你有一定的参考价值。

何时使用值类型

1.如果接受者是一个 map,func 或者 chan,使用值类型(因为它们本身就是引用类型)。
2.如果接受者是一个 slice,并且方法不执行 reslice 操作,也不重新分配内存,使用值类型。
3.如果接受者是一个小的数组或者原生的值类型结构体类型(比如 time.Time 类型),而且没有可修改的字段和指针,又或者接受者是一个简单地基本类型像是 int 和 string,使用值类型就好了

一个值类型的接受者可以减少一定数量的垃圾生成,如果一个值被传入一个值类型接受者的方法,一个栈上的拷贝会替代在堆上分配内存(但不是保证一定成功),所以在没搞明白代码想干什么之前,别因为这个原因而选择值类型接受者。

何时使用指针类型

1.如果方法需要修改接受者,接受者必须是指针类型。
2.如果接受者是一个包含了 sync.Mutex 或者类似同步字段的结构体,接受者必须是指针,这样可以避免拷贝。
3.如果接受者是一个大的结构体或者数组,那么指针类型接受者更有效率。(多大算大呢?假设把接受者的所有元素作为参数传给方法,如果你觉得参数有点多,那么它就是大)。
4.从此方法中并发的调用函数和方法时,接受者可以被修改吗?一个值类型的接受者当方法调用时会创建一份拷贝,所以外部的修改不能作用到这个接受者上。如果修改必须被原始的接受者可见,那么接受者必须是指针类型。
5.如果接受者是一个结构体,数组或者 slice,它们中任意一个元素是指针类型而且可能被修改,建议使用指针类型接受者,这样会增加程序的可读性

当你看完这个还是有疑虑,还是不知道该使用哪种接受者,那么记住使用指针接受者。

  • If the receiver is a map, func or chan, don‘t use a pointer to them. If the receiver is a slice and the method doesn‘t reslice or reallocate the slice, don‘t use a pointer to it.
  • If the method needs to mutate the receiver, the receiver must be a pointer.
  • If the receiver is a struct that contains a sync.Mutex or similar synchronizing field, the receiver must be a pointer to avoid copying.
  • If the receiver is a large struct or array, a pointer receiver is more efficient. How large is large? Assume it‘s equivalent to passing all its elements as arguments to the method. If that feels too large, it‘s also too large for the receiver.
  • Can function or methods, either concurrently or when called from this method, be mutating the receiver? A value type creates a copy of the receiver when the method is invoked, so outside updates will not be applied to this receiver. If changes must be visible in the original receiver, the receiver must be a pointer.
  • If the receiver is a struct, array or slice and any of its elements is a pointer to something that might be mutating, prefer a pointer receiver, as it will make the intention more clear to the reader.
  • If the receiver is a small array or struct that is naturally a value type (for instance, something like the time.Time type), with no mutable fields and no pointers, or is just a simple basic type such as int or string, a value receiver makes sense. A value receiver can reduce the amount of garbage that can be generated; if a value is passed to a value method, an on-stack copy can be used instead of allocating on the heap. (The compiler tries to be smart about avoiding this allocation, but it can‘t always succeed.) Don‘t choose a value receiver type for this reason without profiling first.
  • Finally, when in doubt, use a pointer receiver.

注意:如果方法接受者是基础类型,使用值即可,由于基础类型无法直接取地址,无法自转换为指针模式,使用值接受者即可,其他的都可以使用指针接受者如下会报错:

type INT int

i := &INT(3)

 

参考:

Go Code Review Comments

Go 语言方法接受者类型的选择

 

以上是关于go语言接受者的选取的主要内容,如果未能解决你的问题,请参考以下文章

go语言-interface的值接收和指针接受的区别

go语言-interface的值接收和指针接受的区别

go语言-interface的值接收和指针接受的区别

Go 语言 2019 调查报告发布(内含 Go 语言图谱下载)

Go 语言 2019 调查报告发布(内含 Go 语言图谱下载)

Go 语言 2018 调查报告发布