What does “…” mean when next to a parameter in a go function declaration?

后端 未结 3 1830
旧巷少年郎
旧巷少年郎 2020-12-09 09:17

I was going through some code written in Google\'s Go language, and I came across this:

func Statusln(a ...interface{})
func Statusf(format string, a ...inte         


        
3条回答
  •  再見小時候
    2020-12-09 09:56

    It is variable length argument

    func Printf(format string, v ...interface{}) (n int, err error) {
    

    Take for example this signature. Here we define that we have one string to print, but this string can be interpolated with variable number of things (of arbitrary type) to substitude (actually, I took this function from fmt package):

    fmt.Printf("just i: %v", i)
    fmt.Printf("i: %v and j: %v",i,j)
    

    As you can see here, with variadic arguments, one signature fits all lengths.

    Moreover, you can specify some exact type like ...int.

提交回复
热议问题