我可以为常用方法创建方法别名:
func method1() {
fmt.Println("method1")
}
var Method1 = method1
但不能对方法接收者做同样的事情:
type Person struct {
Name string
}
func (p *Person) methodReciver() {
fmt.Println("method reciver")
}
var MethodReciver = methodReciver
在这种情况下,我在线收到错误消息var MethodReciver = methodReciver:
undefined: methodReciver
完整代码:
package main
import (
"fmt"
)
type Person struct {
Name string
}
func method1() {
fmt.Println("method1")
}
var Method1 = method1
func (p *Person) methodReceiver() {
fmt.Println("method receiver")
}
var MethodReceiver = methodReceiver
func main() {
method1()
Method1()
p := Person{"Nick"}
p.methodReceiver()
p.MethodReceiver()
}
是否可以创建方法别名methodReceiver
?
HUX布斯
LEATH
相关分类