如何创建具有不同签名的函数切片?

如何创建具有不同签名的函数切片?我尝试了下面的代码,但感觉很hack-ish。我们是否只是硬着头皮使用切片接口{}?


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")


}

这只是试图用 Go 做太多事情的一个例子吗?


动漫人物
浏览 108回答 2
2回答

德玛西亚99

请检查一下,我不知道它是否你想要的。因为我不知道你到底想要什么。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "reflect")func A() {&nbsp; &nbsp; fmt.Println("A")}func B(A int) {&nbsp; &nbsp; fmt.Println("B", A)}func C(A string, B float32) {&nbsp; &nbsp; fmt.Println("C", A, B)}func main() {&nbsp; &nbsp; f := []interface{}{A, B, C}&nbsp; &nbsp; f[0].(func())()&nbsp; &nbsp; f[1].(func(int))(15)&nbsp; &nbsp; f[2].(func(string, float32))("TEST", 90)&nbsp; &nbsp; fmt.Println("\n******* another thing ******")&nbsp; &nbsp; for a, v := range f {&nbsp; &nbsp; &nbsp; &nbsp; v := reflect.TypeOf(v)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("#######", a)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("num param :", v.NumIn())&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < v.NumIn(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("param :", i, "type is ", v.In(i))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}检查去游乐场这里我有另一个使用反射调用的示例package main&nbsp; &nbsp; import (&nbsp; &nbsp; &nbsp; &nbsp; "fmt"&nbsp; &nbsp; &nbsp; &nbsp; "reflect"&nbsp; &nbsp; )&nbsp; &nbsp; func A() {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("A")&nbsp; &nbsp; }&nbsp; &nbsp; func B(A int) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("B", A)&nbsp; &nbsp; }&nbsp; &nbsp; func C(A string, B float32) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("C", A, B)&nbsp; &nbsp; }&nbsp; &nbsp; func main() {&nbsp; &nbsp; &nbsp; &nbsp; f := []interface{}{A, B, C}&nbsp; &nbsp; &nbsp; &nbsp; f[0].(func())()&nbsp; &nbsp; &nbsp; &nbsp; f[1].(func(int))(15)&nbsp; &nbsp; &nbsp; &nbsp; f[2].(func(string, float32))("TEST", 90)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("\n******* calling with reflect ******")&nbsp; &nbsp; &nbsp; &nbsp; for a, v := range f {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v := reflect.TypeOf(v)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //calling the function from reflect&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val := reflect.ValueOf(f[a])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params := make([]reflect.Value, v.NumIn())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if v.NumIn() == 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params[0] = reflect.ValueOf(1564)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if v.NumIn() == 2 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params[0] = reflect.ValueOf("Test FROM reflect")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; params[1] = reflect.ValueOf(float32(123456))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val.Call(params)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕桂英3389331

取决于你需要什么不同。根据您的示例,我们可以使用variadic。package mainimport(&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strings")func foo(ss ...string) string{&nbsp; &nbsp; return strings.Join(ss, " ")}func main(){&nbsp; &nbsp; fmt.Println(foo("one"))&nbsp; &nbsp; fmt.Println(foo("one", "two"))&nbsp; &nbsp; fmt.Println(foo("one", "two", "three"))}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go