Junior
Is it possible to pass an arbitrary number of arguments to a function?
sobes.tech AI
Answer from AI
Yes, in Go you can pass an arbitrary number of arguments to a function using variadic parameters. This is done by using the syntax ... before the parameter type in the function declaration. For example:
func sum(nums ...int) int {
total := 0
for _, num := range nums {
total += num
}
return total
}
func main() {
result := sum(1, 2, 3, 4)
fmt.Println(result) // Outputs 10
}
In this example, the sum function accepts any number of integer arguments and returns their sum.