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

后端 未结 3 1841
旧巷少年郎
旧巷少年郎 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:39

    It means that you can call Statusln with a variable number of arguments. For example, calling this function with:

    Statusln("hello", "world", 42)
    

    Will assign the parameter a the following value:

    a := []interface{}{"hello", "world", 42}
    

    So, you can iterate over this slice a and process all parameters, no matter how many there are. A good and popular use-case for variadic arguments is for example fmt.Printf() which takes a format string and a variable number of arguments which will be formatted according to the format string.

提交回复
热议问题