英文:
golang function alias on method receiver
问题
我可以为普通方法创建方法别名:
func method1() {
fmt.Println("method1")
}
var Method1 = method1
但是无法为方法接收器创建相同的别名:
type Person struct {
Name string
}
func (p *Person) methodReceiver() {
fmt.Println("method receiver")
}
var MethodReceiver = methodReceiver
在这种情况下,我在var MethodReceiver = methodReceiver
这一行上得到了错误:
undefined: methodReceiver
完整代码:
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
创建方法别名?
英文:
I can create method alias for an usual method:
func method1() {
fmt.Println("method1")
}
var Method1 = method1
But cannot do the same for a method receiver:
type Person struct {
Name string
}
func (p *Person) methodReciver() {
fmt.Println("method reciver")
}
var MethodReciver = methodReciver
In this case I got the error on line var MethodReciver = methodReciver
:
undefined: methodReciver
Full code:
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()
}
Is it possible to create a method alias for methodReceiver
?
答案1
得分: 35
基本上你有两个选项:
1. 使用方法表达式
它的形式为ReceiverType.MethodName
,并且它返回一个函数类型的值:
var MethodReceiver = (*Person).methodReceiver
MethodReceiver
只保存函数引用,而不保存接收器,所以如果你想调用它,你还需要传递一个接收器(类型为*Person
)作为它的第一个参数:
var p = &Person{"Alice"}
MethodReceiver(p) // 接收器是显式的:p
2. 使用方法值
它的形式为x.MethodName
,其中表达式x
具有静态类型T
:
var p = &Person{"Bob"}
var MethodReceiver2 = p.methodReceiver
方法值也保存了_接收器_,所以当你调用它时,你不需要传递接收器:
MethodReceiver2() // 接收器是隐式的:p
完整示例
在Go Playground上试一试。
type Person struct {
Name string
}
func (p *Person) printName() {
fmt.Println(p.Name)
}
var PrintName1 = (*Person).printName
func main() {
var p1 *Person = &Person{"Bob"}
PrintName1(p1) // 必须显式指定接收器:p1
p2 := &Person{"Alice"}
var PrintName2 = p2.printName // 方法值,也保存了p2
PrintName2() // 隐式接收器:p2
}
输出:
Bob
Alice
英文:
Basically you have 2 options:
1. Using a Method Expression
Which has the form of ReceiverType.MethodName
and it yields a value of a function type:
var MethodReceiver = (*Person).methodReceiver
MethodReceiver
just holds the function reference but not the receiver, so if you want to call it, you also have to pass a receiver (of type *Person
) to it as its fist argument:
var p = &Person{"Alice"}
MethodReceiver(p) // Receiver is explicit: p
2. Using a Method Value
Which has the form of x.MethodName
where the expression x
has a static type T
:
var p = &Person{"Bob"}
var MethodReceiver2 = p.methodReceiver
A method value also stores the receiver too, so when you call it, you don't have to pass a receiver to it:
MethodReceiver2() // Receiver is implicit: p
Complete Example
Try it on Go Playground.
type Person struct {
Name string
}
func (p *Person) printName() {
fmt.Println(p.Name)
}
var PrintName1 = (*Person).printName
func main() {
var p1 *Person = &Person{"Bob"}
PrintName1(p1) // Have to specify receiver explicitly: p1
p2 := &Person{"Alice"}
var PrintName2 = p2.printName // Method value, also stores p2
PrintName2() // Implicit receiver: p2
}
Output:
Bob
Alice
答案2
得分: 18
是的。你可以这样创建一个别名:
var MethodReceiver = (*Person).methodReceiver
当你调用它时,你必须提供一个指向Person对象的指针作为第一个参数:
MethodReceiver(&p)
英文:
Yes. You can make an alias like this:
var MethodReceiver = (*Person).methodReceiver
When you call it, you have to provide a pointer to a person object as the first argument:
MethodReceiver(&p)
答案3
得分: 0
这被称为方法表达式 var MethodReceiver = (*Person).methodReceiver
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论