【发布时间】:2023-04-07 18:54:01
【问题描述】:
据此回复此question
接收者的指针与值的规则是值方法可以在指针和值上调用,但指针方法只能在指针上调用
但实际上我可以对非指针值执行指针方法:
package main
import "fmt"
type car struct {
wheels int
}
func (c *car) fourWheels() {
c.wheels = 4
}
func main() {
var c = car{}
fmt.Println("Wheels:", c.wheels)
c.fourWheels()
// Here i can execute pointer method on non pointer value
fmt.Println("Wheels:", c.wheels)
}
那么,这里有什么问题?这是一个新功能吗?还是对问题的回答有误?
【问题讨论】: