Go方法集 - 指针类型* T与接收者T的调用方法 [英] Go methods sets — Calling method for pointer type *T with receiver T

查看:571
本文介绍了Go方法集 - 指针类型* T与接收者T的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问规范说:


任何其他类型T的方法集由所有具有接收器类型T的方法组成。相应的指针类型* T的方法集是具有接收器* T或T(即,它也包含T)的方法集。


我理解为:T有自己的方法集,而* T有它拥有方法集合和T的方法集合,因为它可以将接收器* T解引用到T并调用方法。因此,我们可以调用一些接收方T的变量类型T的方法。所以我决定验证我的逻辑:



 包主

导入(
fmt
反映


$ User $ struct $ {

func(self * User)SayWat(){
fmt.Println(self)
fmt.Println(reflect.TypeOf(self))
fmt.Println(WAT \\\

}

func main(){
var user User = User {}

fmt.Println(reflect.TypeOf(user),\\\


user.SayWat()
}

http://play.golang.org / p / xMKuLzUbIf



我有点困惑。看起来我可以在T上调用* T的方法吗?我有一个更广泛的示例 http://play.golang.org/p/RROPMj534A ,它也让我困惑。是否有一些反过来类型推断?



我是否缺少某些东西,或者我的逻辑不正确?

谢谢!

解决方案

您不能在<$ * T c $ c> T ,但编译器足够聪明,可以为你引用变量,有效地调用

 (& user).SayWat()

解释此处


调用:如果x
(的类型)的方法集包含m,并且参数列表可以分配给
m的参数列表,则方法调用xm()是有效的。如果x是可寻址的并且& x的方法集包含m,则对于(& x).m(),x.m()是简写


要理解这种差异,您可以举一个返回值(不可寻址):

  func aUser()用户{
返回用户{}
}

...

aUser()。SayWat()

错误:

  prog.go:40:不能调用aUser()的指针方法
prog.go:40:不能取aUser

http ://play.golang.org/p/HOTKiiOK7S


Go spec says:

The method set of any other type T consists of all methods with receiver type T. The method set of the corresponding pointer type *T is the set of all methods with receiver *T or T (that is, it also contains the method set of T).

I understand this as: T has its own method set, while *T has it own method set plus the method set of T, because it can dereference receiver *T to T and call the method. Therefore, we can call some method with receiver *T of variable type T.

So I decided to verify my logic:

package main

import (
  "fmt"
  "reflect"
)

type User struct{}

func (self *User) SayWat() {
  fmt.Println(self)
  fmt.Println(reflect.TypeOf(self))
  fmt.Println("WAT\n")
}

func main() {
  var user User = User{}

  fmt.Println(reflect.TypeOf(user), "\n")

  user.SayWat()
}

http://play.golang.org/p/xMKuLzUbIf

I am a bit confused. It looks like I can call methods "of *T" on T? I have a bit wider example http://play.golang.org/p/RROPMj534A, which confuses me too. Is there some vice versa type inference?

Am I missing something, or my logic is incorrect?

Thanks!

解决方案

You cannot call a method of *T on T, but the compiler is smart enough to take the reference of the variable for you, effectively calling

(&user).SayWat()

This is explained here:

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().

To understand the difference, you can for instance take a return value (non-addressable):

func aUser() User {
    return User{}
}

...

aUser().SayWat()

Fails with error:

prog.go:40: cannot call pointer method on aUser()
prog.go:40: cannot take the address of aUser()

http://play.golang.org/p/HOTKiiOK7S

这篇关于Go方法集 - 指针类型* T与接收者T的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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