简体   繁体   中英

How to pass parameters to decorated functions?

I would like to write a decorator to wrap a function with "before" and "after" commands. A first version is below, where the decorated function just outputs 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")
}

(Playground: https://play.golang.org/p/vJuQKWpZ2h9 )

I now would like to call the decorated function with a parameter (in my case "world" ). In the example above, it is successfully passed to wrapper() but then I do not know what to do further. I thought that I would just

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)
}

The compilation failes with

.\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 ()

What is the proper way to pass arguments to the decorated function?

You have to declare the correct variable type for this to work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM