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

Issue

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 ...interface{})

I don’t understand what the ... means. Does anybody know?

Solution

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.

Answered By – tux21b

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.