golang function alias on method receiver

后端 未结 3 866
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 13:23

I can create method alias for an usual method:

func method1() {
    fmt.Println(\"method1\")
}

var Method1 = method1

But cannot do the sam

3条回答
  •  温柔的废话
    2020-12-01 14:05

    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.

提交回复
热议问题