type-receiver methodpoint-receiver method
func (test *Test) modify() {
test.a++
}
func main() {
test := Test{10}
fmt.Println(test)
test.modify()
fmt.Println(test)
}
我觉得我可以接受。但是当它与界面混合时,事情就出错了。
type Modifiable interface {
modify()
}
type Test struct {
a int
}
func (test *Test) modify() {
test.a++
}
func main() {
test := Test{10}
fmt.Println(test)
test.modify()
fmt.Println(test)
var a Modifiable
a = test
}
它说:
Test does not implement Modifiable (modify method has pointer receiver)
为什么会这样?
golang 实际上是如何处理方法调用的?