现在,在我的代码中,我有一堆重复的工作,看起来像这样.
type FP = func(arg1 string, arg2 int, arg3 []string) bool
func Decorator(fp FP, arg1 string, arg2 int, arg3 []string) {
// Do some stuff prior to running fP
res := fp(arg1, arg2, arg3)
// Do some other stuff depending on res
}
我的问题是,每次我想用不同数量/类型的参数添加一个新的FP类型时,我都必须重新定义这个方法,所以我最终在我的代码中重新定义了4-5次修饰符,可能需要做更多的工作.我想把装修工换成
type FP = func(a ...interface{}) bool
func Decorator(fp FP, argDetails interface{}) {
// do stuff
res := fp(a)
// do other stuff
}
并让FP断言类型.但我不确定这是否会导致问题/是一个糟糕的用例,因为我看到了很多关于不使用接口/会导致性能问题的东西.