小编典典

Golang-如何创建具有不同签名的函数片段?

go

如何创建具有不同签名的函数片段?我尝试了下面的代码,但感觉有点黑。我们只是硬着头皮使用切片界面{}吗?

package main

import (
    "fmt"
)

type OneParams func(string) string
type TwoParams func(string, string) string
type ThreeParams func(string, string, string) string

func (o OneParams) Union() string {
    return "Single string"
}

func (t TwoParams) Union() string {
    return "Double string"
}

func (t ThreeParams) Union() string {
    return "Triple string"
}

type Functions interface {
    Union() string
}

func Single(str string) string {
    return str
}

func Double(str1 string, str2 string) string {
    return str1 + " " + str2
}

func Triple(str1 string, str2 string, str3 string) string {
    return str1 + " " + str2 + " " + str3
}

func main() {
    fmt.Println("Slice Of Functions Program!\n\n")

    fSlice := []Functions{
        OneParams(Single),
        TwoParams(Double),
        ThreeParams(Triple),
    }

    for _, value := range fSlice {
        switch t := value.(type) {
        case OneParams:
            fmt.Printf("One: %s\n", t("one"))
        case TwoParams:
            fmt.Printf("Two: %s\n", t("one", "two"))
        case ThreeParams:
            fmt.Printf("Three: %s\n", t("one", "two", "three"))
        default:
            fmt.Println("Huh! What's that?")
        }
    }

    fmt.Println("\n\n")

}

这仅仅是尝试对Golang做太多事情的一种情况吗?


阅读 222

收藏
2020-07-02

共1个答案

小编典典

请检查它,我不知道它是否想要什么。因为我不知道你到底想要什么。

package main

import (
    "fmt"
    "reflect"
)

func A() {
    fmt.Println("A")
}

func B(A int) {
    fmt.Println("B", A)
}

func C(A string, B float32) {
    fmt.Println("C", A, B)
}

func main() {
    f := []interface{}{A, B, C}
    f[0].(func())()
    f[1].(func(int))(15)
    f[2].(func(string, float32))("TEST", 90)

    fmt.Println("\n******* another thing ******")
    for a, v := range f {
        v := reflect.TypeOf(v)
        fmt.Println("#######", a)
        fmt.Println("num param :", v.NumIn())
        for i := 0; i < v.NumIn(); i++ {
            fmt.Println("param :", i, "type is ", v.In(i))
        }
    }
}

Go
Playground
上检查

在这里,我有另一个使用反射调用的示例

package main

    import (
        "fmt"
        "reflect"
    )

    func A() {
        fmt.Println("A")
    }

    func B(A int) {
        fmt.Println("B", A)
    }

    func C(A string, B float32) {
        fmt.Println("C", A, B)
    }

    func main() {
        f := []interface{}{A, B, C}
        f[0].(func())()
        f[1].(func(int))(15)
        f[2].(func(string, float32))("TEST", 90)

        fmt.Println("\n******* calling with reflect ******")
        for a, v := range f {
            v := reflect.TypeOf(v)
            //calling the function from reflect
            val := reflect.ValueOf(f[a])
            params := make([]reflect.Value, v.NumIn())
            if v.NumIn() == 1 {
                params[0] = reflect.ValueOf(1564)
            } else if v.NumIn() == 2 {
                params[0] = reflect.ValueOf("Test FROM reflect")
                params[1] = reflect.ValueOf(float32(123456))
            }
            val.Call(params)
        }
    }

Go
Playground
上检查

2020-07-02