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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With