golang function alias on method receiver

huangapple go评论90阅读模式
英文:

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()
}

Playground

是否可以为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()
}

Playground

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)

你可以在Go Playground上看到它的实际效果

英文:

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)

You can see this in action on the Go Playground.

答案3

得分: 0

这被称为方法表达式 var MethodReceiver = (*Person).methodReceiver

Playground

英文:

This is called method expression var MethodReceiver = (*Person).methodReceiver

Playground

huangapple
  • 本文由 发表于 2015年1月31日 19:53:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/28251283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定