0

使用此代码作为模板

package main

import "fmt"

type myStruct struct {
    Value int
}

type counter int

func newFuncHandler(fn func(myStruct) error) (interface{}, *counter) {
    count := counter(0)

    newFn := func(e myStruct) error {
        count = count + 1
        return fn(e)
    }

    return newFn, &count
}

func main() {
    fn := func(d myStruct) error {
        // doing some stuff
        return nil
    }

    handle, c := newFuncHandler(fn)
    handleFn := handle.(func(d myStruct) error)

    handleFn(myStruct{Value: 2})
    handleFn(myStruct{Value: 2})
    handleFn(myStruct{Value: 2})
    handleFn(myStruct{Value: 2})
    handleFn(myStruct{Value: 2})

    fmt.Println(*c) // 5
}

如何修改newFuncHandler,以便在给定具有未知签名的函数的情况下,返回具有相同签名但具有函数体附加代码的函数。newFuncHandler不应该知道myStruct类型

例如

func newFuncHandler(fn interface{}) (interface{}, *counter) {
    count := counter(0)
    // some reflection magic

    // newFn has the same signature as fn (hardcoded in this case)
    newFn := func(e myStruct) error {
        // add custom code 
        count = count + 1

        // call the original fn
        return fn(e)
    }

    return newFn, &count
}

4

1 回答 1

2

使用reflect.MakeFunc创建一个函数。使用Value.Call调用函数。

func newFuncHandler(v interface{}) (interface{}, *counter) {
    count := counter(0)
    fn := reflect.ValueOf(v)
    newFn := reflect.MakeFunc(fn.Type(), func(args []reflect.Value) (results []reflect.Value) {
        count = count + 1
        return fn.Call(args)
    })
    return newFn.Interface(), &count
}

在操场上运行它

于 2020-04-03T07:43:10.907 回答