How do I declare a function pointer to a method in Go

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

How do I declare a function pointer to a method in Go

问题

我正在尝试创建一个具有方法接收器的函数指针,但是我无法弄清楚如何使其工作(如果可能的话)?

基本上,我有以下代码:

type Foo struct {...}
func (T Foo) Bar bool {
   ... 
}

type BarFunc (Foo) func() bool // 这行代码无法工作。

代码的最后一行会报错:

语法错误:意外的 func,期望分号或换行符
英文:

I am trying to create function pointer to a function that has a method receiver. However, I can't figure out how to get it to work (if it is possible)?

Essentially, I have the following:

type Foo struct {...}
func (T Foo) Bar bool {
   ... 
}

type BarFunc (Foo) func() bool // Does not work.

The last line of the code gives the error

syntax error: unexpected func, expecting semicolon or newline

答案1

得分: 23

如果你想创建一个指向方法的函数指针,有两种方法。第一种方法是将一个带有一个参数的方法转换为一个带有两个参数的函数:

type Summable int

func (s Summable) Add(n int) int {
    return s+n
}

var f func(s Summable, n int) int = (Summable).Add

// ...
fmt.Println(f(1, 2))

第二种方法是将 s 的值(在评估时)与 Summable 接收器方法 Add "绑定",然后将其赋值给变量 f

s := Summable(1)
var f func(n int) int = s.Add
fmt.Println(f(2))

Playground: http://play.golang.org/p/ctovxsFV2z.

f 被赋值后对 s 进行的任何更改都不会影响结果:https://play.golang.org/p/UhPdYW5wUOP

英文:

If you want to create a function pointer to a method, you have two ways. The first is essentially turning a method with one argument into a function with two:

type Summable int

func (s Summable) Add(n int) int {
    return s+n
}

var f func(s Summable, n int) int = (Summable).Add

// ...
fmt.Println(f(1, 2))

The second way will "bind" the value of s (at the time of evaluation) to the Summable receiver method Add, and then assign it to the variable f:

s := Summable(1)
var f func(n int) int = s.Add
fmt.Println(f(2))

Playground: http://play.golang.org/p/ctovxsFV2z.

Any changes to s after f is assigned will have no affect on the result: https://play.golang.org/p/UhPdYW5wUOP

答案2

得分: 2

以下是代码的中文翻译:

package main

import "fmt"

type DyadicMath func(int, int) int  // 你的函数指针类型

func doAdd(one int, two int) (ret int) {
	ret = one + two;
	return
}

func Work(input []int, addthis int, workfunc DyadicMath) {
	for _, val := range input {
		fmt.Println("--> ", workfunc(val, addthis))
	}
}

func main() {
	stuff := []int{1, 2, 3, 4, 5}
	Work(stuff, 10, doAdd)

	doMult := func(one int, two int) (ret int) {
		ret = one * two;
		return
	}
	Work(stuff, 10, doMult)

}

你可以在这里查看代码:https://play.golang.org/p/G5xzJXLexc

英文:

And for an example more familiar to those of us used to a typedef in C for function pointers:

package main

import "fmt"

type DyadicMath func (int, int) int  // your function pointer type

func doAdd(one int, two int) (ret int) {
	ret = one + two;
	return
}

func Work(input []int, addthis int, workfunc DyadicMath) {
	for _, val := range input {
		fmt.Println("--> ",workfunc(val, addthis))
	}
}

func main() {
	stuff := []int{ 1,2,3,4,5 }
	Work(stuff,10,doAdd)

	doMult := func (one int, two int) (ret int) {
		ret = one * two;
		return
	}	
	Work(stuff,10,doMult)
	
}

https://play.golang.org/p/G5xzJXLexc

答案3

得分: 0

我很可能偏离目标(刚开始学习Golang),但如果你创建一个指针然后检查类型,会怎么样呢:

pfun := Bar 
fmt.Println("pfun的类型是:", reflect.TypeOf(pfun))

然后看起来你可以正确声明指针的类型:
https://play.golang.org/p/SV8W0J9JDuQ

英文:

I am very likely off-target (just started on Golang) but what if you create a pointer then examine type:

pfun := Bar 
fmt.Println("type of pfun is:", reflect.TypeOf(pfun))

then it seems that you can declare the type of pointer correctly:
https://play.golang.org/p/SV8W0J9JDuQ

huangapple
  • 本文由 发表于 2015年7月22日 19:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/31561369.html
匿名

发表评论

匿名网友

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

确定