Go 创建一个动态函数

Go 创建一个动态函数,go,Go,我需要在Golang实现一个现有的逻辑。一般来说,它是类似于: // Function receives an object with a variable type (struct) func(dynObject interface{}) { var variableFunc ... // What should be here? // Some things to do depending on the input param type switch dynObj

我需要在Golang实现一个现有的逻辑。一般来说,它是类似于:

// Function receives an object with a variable type (struct)
func(dynObject interface{}) {

    var variableFunc ... // What should be here?

    // Some things to do depending on the input param type
    switch dynObject.(type) {
    case *objA:
        // do some job
    case *objB:
        // do some job
    }

    // ...
    // Creating and calculating some variables for the following loop
    // ...

    humans := []*Humans{ ....... }
    sum := []int{}

    for _, human := range humans {
        result := variableFunc(dynObject, human)
        sum = append(sum, result)
    }
}
如您所见,作为输入,有一个dynObject,它是一个结构,但可以是一些预定义的结构中的任何一个。后面有一个函数variableFunc,它必须接受这个对象和进程,处理逻辑也取决于类型。因此,类型objA需要与objB不同的逻辑

我不知道一个正常的方法来实现这一点。我如何创建这个动态函数,它可以将不同的结构作为输入,并对其应用所需的逻辑?当我得到一个错误时,该函数期望另一个类型作为输入。我希望避免额外的开关案例,因为已经存在一个

当然,我试图创建一个预定义的函数,并使用接口,然后在现有的开关盒中重新定义它,但没有成功。我需要你的帮助

package main

import "fmt"

func main() {
    common(4)
    common("whitespace")
}
func common(a interface{}) {
    switch a.(type) {
    case int:
        div(a.(int), 2)
        break
    case string:
        sayHello(a.(string))
        break
    default:
        //nothing
    }
    return
}
func div(a, b int) {
    fmt.Printf("%d", a/b)
    return
}
func sayHello(a string) {
    fmt.Printf("%s", a)
}
main函数调用一个公共函数,这又调用两个不同的函数,它们具有不同的函数签名。您可以看到它们执行不同的操作。因此,这将为不同的输入打印不同的结果。如果在switch语句中调用不同的函数是您想要做的,我可以从您的评论中收集到很多信息,那么这应该可以做到。让我知道

在我的switch案例中有一项工作要做,然后它的结果被用来计算我的示例中间部分的其他变量,只有在这之后我才调用variableFunc

您可以使用defer。所以,这不是延迟的常规用法。但从技术上讲,它可以用于类似的事情。上述功能只有在//完成某项工作后才能执行,我相信这正是您所追求的灵活性。如果某些函数陷入恐慌,则该语句将处于执行和退出之间。所以,你必须知道你在做什么

或者您可以使用它,以防在稍后阶段输入参数

package stackoverflow

import "fmt"

func common(a interface{}) {
    var f func(interface{}, int)
    switch a.(type) {
    case int:
        f = func(s interface{}, b int) {
            fmt.Printf("%d and %d\n", s.(int)/2, b)
        }
        break
    case string:
        f = func(b interface{}, c int) {
            fmt.Printf("hello %s and %d \n", b.(string), c)
        }
        break
    default:
        //nothing
    }
    //do something here
    fmt.Println("did something here test")
    // the function gets called after
    var x int = 21

    f(a, x)

    return
}

到目前为止,您正在创建一个函数,该函数将接受一个接口,并根据类型切换代码。你不应该得到任何类型的错误,在我得到错误的那一刻,该函数需要另一个类型作为输入,你能复制粘贴你在这里得到的错误吗?如果variableFunc是动态的,也许它能帮助查看它的伪代码或逻辑?对不起,我不太明白你的意思。@vdolez,我相信在这种情况下这并不重要,想象一下他们是不同的。像objA需要objA.x+objA.y,但是objB需要objB.z*150-objB。n@Alexey为什么?如果variableFunc是一个用于处理特定类型的函数,那么可以通过断言接口的类型来完成。另一方面,如果要在另一个动态函数中创建多接口变量类型函数,那么最终将使用更多的switch语句。所以这里的问题是,封装函数和variableFunc都是动态的,还是封装函数是静态的,而variableFunc是动态的?你不能在每个结构中添加两个函数,比如DoWork和HandleHuman*Human int,它们实现了结构特定的逻辑吗?然后您还可以创建一个接口,其中包含这些函数,并且您的基函数可以接受该接口类型的参数。然后你所要做的就是调用这两个函数。这种方法还去掉了第一个开关,因为您只需调用DoWork函数.Close。但您忽略了一些事情:在我的switch case中有一个任务要做,然后在我的示例的中间部分,它的结果被用来计算其他变量,只有在这之后,我才调用variableFunc。在您的示例中,我不能这么快调用div或sayHello,因为还不知道所有需要的参数。在我的情况下,这就像一个接一个的切换,我希望至少避免第二个切换。
package stackoverflow

import "fmt"

func common(a interface{}) {
    var f func(interface{}, int)
    switch a.(type) {
    case int:
        f = func(s interface{}, b int) {
            fmt.Printf("%d and %d\n", s.(int)/2, b)
        }
        break
    case string:
        f = func(b interface{}, c int) {
            fmt.Printf("hello %s and %d \n", b.(string), c)
        }
        break
    default:
        //nothing
    }
    //do something here
    fmt.Println("did something here test")
    // the function gets called after
    var x int = 21

    f(a, x)

    return
}