这样理解是正确的么?该怎么理解?

Aruforce · · 801 次点击
"person2.SetAge(11);//这里应该编译不过去啊,因为SetAge接收的是*Person类型,而person2是Person类型": `If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m()` "person3.SetAge2(11);//这里应该编译不过去啊,因为SetAge接收的是Person类型啊,person3是*Person类型": `A method call x.m() is valid if the method set of (the type of) x contains m` `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).` https://golang.google.cn/ref/spec#Calls A method call x.m() is valid if the method set of (the type of) x contains m and the argument list can be assigned to the parameter list of m. If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m() https://golang.google.cn/ref/spec#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). https://golang.google.cn/doc/effective_go.html#pointers_vs_values There is a handy exception, though. When the value is addressable, the language takes care of the common case of invoking a pointer method on a value by inserting the address operator automatically. In our example, the variable b is addressable, so we can call its Write method with just b.Write. The compiler will rewrite that to (&b).Write for us.
#4
更多评论
理解大概是对的, SetId()不会和(person *Person) SetId()冲突, go会自动为类型推导其指针,非指针方法,
#1
并不是这一段有疑问; 有疑问的地方在:SetAge和SetAge2 按照方法的定义,SetAge 压栈时一部压入 *Person类型,而SetAge2 压栈时必须压入Person类型, 在上面代码person2.SetAge(11)和person3.SetAge2(11)编译或者运行时应该报错,我认为应该报错的原因正如注释缩写; 然而结果却没有,然后就可是迷惑了.
#2