Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 968
Chaos Avatar asked Apr 12 '12 17:04

Chaos


People also ask

How are parameters passed in Go?

Golang supports two different ways to pass arguments to the function i.e. Pass by Value or Call by Value and Pass By Reference or Call By Reference. By default, Golang uses the call by value way to pass the arguments to the function.

What is := in Golang?

short declaration operator. var is a lexical keyword present in Golang. := is known as the short declaration operator. It is used to declare and initialize the variables inside and outside the functions. It is used to declare and initialize the variables only inside the functions.

How do functions work in Go?

A function in Go is also a type. If two function accepts the same parameters and returns the same values, then these two functions are of the same type. For example, add and substract which individually takes two integers of type int and return an integer of type int are of the same type.


1 Answers

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.

like image 159
tux21b Avatar answered Sep 23 '22 10:09

tux21b