Golang方法集(指针与值接收器) [英] Golang Method Sets (Pointer vs Value Receiver)

查看:864
本文介绍了Golang方法集(指针与值接收器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解为什么这些规则与指针类型.vs的方法集相关联。值类型

可以请某人解释原因(从界面表的角度来看)



(William Kennedy's博客)

 值方法接收者
----------------- ------------------------------
T(t T)
* T(t T)和(t * T)

方法接收者值
------------------------------ -----------------
(t T)T和* T
(t * T)* T

规范片段



方法集合

类型可能有与之相关的方法集。接口类型的方法集是它的接口。
任何其他类型T的方法集合由所有用接收方类型T声明的方法组成。相应的指针类型* T的方法集合是用接收方* T或T声明的所有方法的集合(即它还包含T)的方法集合。其他规则适用于包含匿名字段的结构,如结构类型部分所述。任何其他类型都有一个空方法集。在一个方法集中,每个方法必须有一个唯一的非空白方法名称。



类型的方法集决定了类型实现的接口以及可以如果你有一个 * T 您可以调用接收方类型为 * T 的方法以及接收方类型为 T (您引用的段落,方法集)。

  • 如果您有 T ,且可寻址您可以调用接收方类型为 * T 的方法以及接收方类型为 T 的方法,因为方法调用 t.Meth()将等于(& t).Meth()

  • 如果您有一个 T code> a nd它不可寻址,您只能调用接收器类型为 T ,而不是 * T 的方法。

  • 如果您有一个接口 I ,并且 I 的方法集由接收方 * T 的方法提供(其余部分由接收方为 T ),那么 * T 满足界面 I ,但 T 不。这是因为 * T 的方法集合包含 T 的,但并非相反(回到第一点)。

    简而言之,您可以将方法与具有指针接收器的值接收器和方法混合并匹配,并将它们与包含值和指针的变量,而不用担心哪个是哪个。两者都可以工作,语法也是一样的。但是,如果需要带指针接收器的方法来满足接口,那么只有一个指针可以分配给接口 - 一个值将无效。


    I am having a hard time understanding as to why are these rules associated with method set of pointer type .vs. value type

    Can someone please explain the reason (from the interface table perspective)

    (Snippet from William Kennedy's blog)

    Values          Methods Receivers
    -----------------------------------------------
    T               (t T)
    *T              (t T) and (t *T)
    
    Methods Receivers    Values
    -----------------------------------------------
    (t T)                 T and *T
    (t *T)                *T
    

    Snippet from specification

    Method sets

    A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T). Further rules apply to structs containing anonymous fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.

    The method set of a type determines the interfaces that the type implements and the methods that can be called using a receiver of that type.

    解决方案

    1. If you have a *T you can call methods that have a receiver type of *T as well as methods that have a receiver type of T (the passage you quoted, Method Sets).
    2. If you have a T and it is addressable you can call methods that have a receiver type of *T as well as methods that have a receiver type of T, because the method call t.Meth() will be equivalent to (&t).Meth() (Calls).
    3. If you have a T and it isn't addressable, you can only call methods that have a receiver type of T, not *T.
    4. If you have an interface I, and some or all of the methods in I's method set are provided by methods with a receiver of *T (with the remainder being provided by methods with a receiver of T), then *T satisfies the interface I, but T doesn't. That is because *T's method set includes T's, but not the other way around (back to the first point again).

    In short, you can mix and match methods with value receivers and methods with pointer receivers, and use them with variables containing values and pointers, without worrying about which is which. Both will work, and the syntax is the same. However, if methods with pointer receivers are needed to satisfy an interface, then only a pointer will be assignable to the interface — a value won't be valid.

    这篇关于Golang方法集(指针与值接收器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆