Go 如何将参数传递给修饰函数?

Go 如何将参数传递给修饰函数?,go,parameters,decorator,Go,Parameters,Decorator,我想编写一个装饰程序,用“before”和“after”命令包装函数。下面是第一个版本,其中修饰函数只输出hello: package main import "fmt" func main() { wrapper(printHello, "world") } func wrapper(f func(), who string) { fmt.Printf("before function, sending %v\n"

我想编写一个装饰程序,用“before”和“after”命令包装函数。下面是第一个版本,其中修饰函数只输出
hello

package main

import "fmt"

func main() {
    wrapper(printHello, "world")
}

func wrapper(f func(), who string) {
    fmt.Printf("before function, sending %v\n", who)
    f()
    fmt.Print("after function\n")
}

func printHello() {
    fmt.Printf("hello\n")
}
(游乐场:)

现在,我想用一个参数调用修饰函数(在我的例子中是
“world”
)。在上面的示例中,它被成功地传递到
wrapper()
,但是我不知道接下来要做什么。我以为我会

package main

import "fmt"

func main() {
    wrapper(printHello, "world") // cannot use printHello as the type func()
}

func wrapper(f func(), who string) {
    fmt.Printf("before function, sending %v\n", who)
    f(who) // too many arguments
    fmt.Print("after function\n")
}

func printHello(who string) {
    fmt.Printf("hello %v\n", who)
}
汇编失败了

.\scratch_11.go:6:9: cannot use printHello (type func(string)) as type func() in argument to wrapper
.\scratch_11.go:11:3: too many arguments in call to f
    have (string)
    want ()

向修饰函数传递参数的正确方法是什么?

您必须声明正确的变量类型才能使用:

func wrapper(f func(string), who string) {
...

声明正确的类型:
func包装器(f func(string),who string)
@BurakSerdar:ah是的,现在这是显而易见的。谢谢。如果你不介意把评论变成一个答案,我很乐意投赞成票并接受它。