如果不能给函数指针赋值,Go 中的函数指针有什么用?

What are function pointers good for in Go if you can't assign them values?

你能用 *func() 在 Go 中做任何事情吗?

    var f func() = foo // works
    var g *func() // works
    g = foo // fails `cannot use foo (type func()) as type *func() in assignment` as expected
    g = &foo // fails too `cannot take the address of foo`

不能取函数定义的地址,可以取函数的地址。这有效:

g := &f

游乐场示例:https://play.golang.org/p/BokYCrVmV_p

你可以传递它并设置它:

func a(f *func()) {
  *f = foo
}

func main() {
   var f func()

   a(&f)
   f()
}