Variadic Functions
Variadic Functions
A variadic function accepts a variable number of arguments. You declare one by placing ... before the parameter type:
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
Inside the function, nums is a regular []int slice. You can call it with any number of arguments:
sum(1, 2, 3) // 6
sum(10, 20) // 30
sum() // 0
Passing a Slice with ...
If you already have a slice, you can spread it into a variadic call using ...:
numbers := []int{1, 2, 3, 4}
total := sum(numbers...)
This is the same ... token, but used on the caller side. Without it, the compiler will reject the call --- you cannot pass a slice where individual arguments are expected.
Common Variadic Functions
You have already been using variadic functions. fmt.Println accepts ...any:
fmt.Println("hello", 42, true) // hello 42 true
The built-in append is also variadic --- it appends one or more elements to a slice:
s := []int{1, 2}
s = append(s, 3, 4, 5)
You can even append one slice to another:
a := []int{1, 2}
b := []int{3, 4}
a = append(a, b...)
Rules
- Only the last parameter can be variadic.
- A variadic function can be called with zero arguments for the variadic parameter.
- The variadic parameter is
nil(not an empty slice) when no arguments are passed.
Your Task
Write a function joinWithSep that takes a string separator followed by a variadic ...string parameter. It should return a single string with all the strings joined by the separator.
For example: joinWithSep(", ", "a", "b", "c") returns "a, b, c".
If no strings are passed, return an empty string.