您的位置:首页 > 编程语言 > Go语言

GO接口赋值与方法接收者问题

2018-07-09 23:37 155 查看

这篇博客是受到GO语言中文网论坛中问题启发所写,原链接在此https://studygolang.com/topics/5946

其中第一个回复为博主

有代码如下:

package main

import "fmt"

type TestStruct struct {
id   int
name string
}

type AnotherTestStruct struct {
id   int
name string
}

type ITest interface {
SayHello()
}

func main() {
var itest ITest
test := TestStruct{id: 1, name: "test1"}
test2 := AnotherTestStruct{id: 2, name: "anotherTest"}
itest = test     // OK
itest.SayHello() // test1

itest = &test    // OK
itest.SayHello() // test1

itest = test2    // cannot use test2 (type AnotherTestStruct) as type ITest in assignment:
// AnotherTestStruct does not implement ITest (SayHello method has pointer receiver)
itest.SayHello()

itest = &test2	 // OK
itest.SayHello() // anotherTest
}

func (test TestStruct) SayHello() {
fmt.Println(test.name)
}

func (test *AnotherTestStruct) SayHello() {
fmt.Println(test.name)
}
其实这里是GO的一种简略的写法,以值为接收者的方法被调用时,接收者既能为值又能为指针。但是以指针为接收者的方法,接收者就只能是指针了。所以&test2作为指针,实现了SayHello方法,所以可以将&test2赋值给itest,而test2作为AnotherTestStruct类型,未实现SayHello方法,不能将其赋值给itest。而&test也可以作为func (test TestStruct) SayHello()方法的接收者,即实现了SayHello方法,所以可以赋值给itest。

至于原因的话,我个人理解是类似传引用与传值的区别,当定义接收者为指针时,相当于是传引用调用,所以必须用指针作为接收者。如果接收者不是指针的话,则即可以利用传值也可以利用传引用,所以上述例子中itest = &test, itest.SayHello()的时候会被解释为(*itest).SayHello()。

如果有不同见解,欢迎讨论指正。


阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐