Golang - How do you create a slice of functions with different signatures?

后端 未结 2 1157
旧时难觅i
旧时难觅i 2021-01-18 01:01

How do you create a slice of functions with different signatures? I tried the code below but its feels hack-ish. Do we just bite the bullet and use a slice interface{}?

2条回答
  •  孤独总比滥情好
    2021-01-18 01:47

    depends on what the different you need. upon your example, we could use variadic.

    package main
    
    import(
        "fmt"
        "strings"
    )
    
    func foo(ss ...string) string{
        return strings.Join(ss, " ")
    }
    
    func main(){
        fmt.Println(foo("one"))
        fmt.Println(foo("one", "two"))
        fmt.Println(foo("one", "two", "three"))
    }
    

提交回复
热议问题