Go:指向带接收器的函数的函数指针

Go:指向带接收器的函数的函数指针,go,Go,我是否可以将函数指针设置为比在其周围创建函数更简单的带接收器的函数 package main import "fmt" type hello struct { name string } func (obj *hello) hello() { fmt.Printf("Hello %s\n", obj.name) } func ntimes(action func (), n int) { for i := 0; i < n; i++ { action() }

我是否可以将函数指针设置为比在其周围创建函数更简单的带接收器的函数

package main

import "fmt"

type hello struct {
  name string
}

func (obj *hello) hello() {
  fmt.Printf("Hello %s\n", obj.name)
}

func ntimes(action func (), n int) {
  for i := 0; i < n; i++ {
    action()
  }
}

func main() {
  obj := hello{"world"}
  // Can I do following simpler?
  ntimes(func() {obj.hello();}, 3)
}
主程序包
输入“fmt”
键入hello struct{
名称字符串
}
func(obj*hello)hello(){
fmt.Printf(“你好%s\n”,对象名称)
}
函数时间(操作函数(),n int){
对于i:=0;i
现在不行。但使用Go 1.1,这将是可能的


Go 1.1将在线路触零时准备就绪。

@系统已编辑,
say
hello
。我想要简化的是在调用
ntimes
时消除匿名函数。不,你不能。如果您总是知道要调用的方法,然后只传入对象,或者可以像在示例中那样将该方法包装在匿名函数中,那么可以使用接口。