go - 在 Go 中将任意函数作为参数传递

我正在尝试扩展我对 Go 函数指针的了解,我有一个问题,关于在 Go 中将函数作为参数传递什么是可能的,什么是不可能的。

假设我想编写一个可以包装任何现有函数的decorator() 函数。为简单起见,我们将其限制为仅接受一个参数并返回一个值的函数。

如果我编写一个接受 func(interface{}) interface{} 作为参数的装饰器,只要我传入的函数也接受/返回一个 ,它就会隐式工作interface{} 类型(参见 funcA)。

我的问题是——有没有一种方法可以将 func(string) string 类型的现有函数转换为 func(interface{}) 接口(interface)类型{} 这样它也可以被传递到装饰器函数中,而不只是将它包装在一个新的匿名函数中(参见 funcB)?

package main

import (
    "fmt"
)

func decorate(inner func(interface{}) interface{}, args interface{}) interface {} {
    fmt.Println("Before inner")
    result := inner(args)
    fmt.Println("After inner")
    return result
}

func funcA(arg interface{}) interface{} {
    fmt.Print("Inside A, with arg: ")
    fmt.Println(arg)
    return "This is A's return value"
}

func funcB(arg string) string {
    fmt.Print("Inside B, with arg: ")
    fmt.Println(arg)
    return "This is B's return value"
}

func main() {
    
    // This one works. Output is:
    //
    //   Before inner
    //   Inside A, with arg: (This is A's argument)
    //   After inner
    //   This is A's return value
    //
    fmt.Println(decorate(funcA, "(This is A's argument)"))
    
    // This doesn't work. But can it?
    //fmt.Println(decorate(funcB, "(This is B's argument)"))
}

最佳答案

这是不可能的。原因之一是传递参数的机制因函数而异,使用 interface{} arg 并不意味着“接受任何东西”。例如,将结构作为 arg 的函数将接收该结构的每个成员,但是将包含该结构的接口(interface){}作为参数的函数将接收两个单词,一个包含结构的类型,另一个包含指向的指针

因此,在不使用泛型的情况下,实现这一点的唯一方法是使用适配器函数。

https://stackoverflow.com/questions/69759462/

相关文章:

python - 如何并排放置两个或多个 ASCII 图像?

r - 如何从 R 中的多列创建合并值的新数据框

c++ - 在 C++ 中迭代枚举类的常用方法是什么?

kotlin - 在 Android Compose 的 View 模型中使用 State

sorting - 如何按索引对 Vec 进行排序?

r - flextable 中有条件的粗体值

google-cloud-platform - 如何继承访问权限以查看属于 GCP 组织的所有项目?

java - 从 Double.toLongBits 创建的 long 中获取 double

postgresql - 在 PostgreSQL 中存储 UUID 的最佳数据类型是什么?

javascript - React 中的 for 循环