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

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

问题描述

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

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

规范摘录

方法集

类型可能具有与之关联的方法集.接口类型的方法集是其接口. 任何其他类型T的方法集都包含以接收者类型T声明的所有方法.相应指针类型* T的方法集是使用接收者* T或T声明的所有方法的集合(也就是说,它还包含方法T集).进一步的规则适用于包含匿名字段的结构,如有关结构类型的部分中所述.其他任何类型的方法集都为空.在方法集中,每个方法必须具有唯一的非空白方法名称.

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. 如果您有*T,则可以调用接收者类型为*T的方法,也可以调用接收者类型为T的方法(您引用的段落可寻址,则可以调用接收者类型为以及接收方类型为T的方法,因为方法调用t.Meth()将等效于(&t).Meth()(通话).
  2. 如果您有一个T并且它不可寻址,则只能调用接收者类型为T的方法,而不能调用*T.
  3. 如果您具有接口I,并且I的方法集中的某些或所有方法是由具有*T的接收器的方法提供的(其余部分由具有接收器的*T的方法提供) T),则*T满足接口I,但T不满足.这是因为*T的方法集包括T的方法集,但没有相反的方法(再次回到第一点).
  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.

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

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