创建一个动态函数

问题描述 投票:-4回答:1

我需要在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不同的逻辑。

我不知道实现这个的正常方法。如何创建这个动态函数,它可以将不同的结构作为输入并应用所需的逻辑?目前我收到错误,该函数需要另一种类型作为输入。我希望避免使用其他开关盒,因为已经存在。

当然我试图创建一个预定义的函数,并使用接口,然后以某种方式在现有的switch-case内重新定义它,但没有运气。我需要你的帮助

go
1个回答
0
投票
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函数调用common函数,这又调用两个不同的函数,它们具有不同的函数签名。您可以看到他们执行不同的操作。因此,这将为不同的输入打印不同的结果。如果在你的switch语句中调用不同的函数是你想要做的(尽管我可以从你的评论中收集到),那么这应该这样做。让我知道。


在我的switch-case中有一项工作要做,然后它的结果用于计算其他变量(在我的例子中的中间部分),之后我只调用variableFunc()

package stackoverflow

import "fmt"

func common(a interface{}) {
    switch a.(type) {
    case int:
        defer func(b int) {
            fmt.Printf("%d \n", b/2)
        }(a.(int))
        break
    case string:
        defer func(b string) {
            fmt.Printf("hello %s \n", b)
        }(a.(string))
        break
    default:
        //nothing
    }

    //do something here
    fmt.Println("did something here test")
    // the function gets called after
    return
}

你可以使用延迟。所以,这不是延迟的传统用法。但从技术上讲,它可以用于这样的事情。上面的功能只有在//something完成后才会执行,我相信这是你所追求的那种灵活性。如果某些功能发生混乱,此语句将在执行和退出之间进入。所以,你必须知道你在做什么。


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

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
}
© www.soinside.com 2019 - 2024. All rights reserved.