Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
In Go, a variadic function is a function that accepts a variable number of arguments of a specific type. The fmt.Println()
function is a common example of a variadic function, as it can take any number of arguments.
This tutorial will guide you through the creation and usage of variadic functions in Go.
A variadic function is declared with an ellipsis ...
before the type name of the last parameter. This indicates that the function can accept zero or more of those parameters.
func sum(nums ...int) int { total := 0 for _, num := range nums { total += num } return total }
You can call a variadic function with any number of trailing arguments:
fmt.Println(sum(1, 2)) // 3 fmt.Println(sum(1, 2, 3, 4)) // 10
If you already have multiple values in a slice, you can pass them to a variadic function using the ellipsis ...
suffix:
nums := []int{1, 2, 3, 4, 5} fmt.Println(sum(nums...)) // 15
You can mix variadic parameters with regular parameters. However, the variadic parameter must always be the last parameter.
func printWithPrefix(prefix string, values ...string) { for _, value := range values { fmt.Println(prefix, value) } } printWithPrefix("INFO:", "Server started", "User logged in")
Variadic functions are useful when:
fmt.Println()
....
prefix before the type of the last parameter....
suffix.Variadic functions offer a flexible way to handle functions with a variable number of arguments, making your code cleaner and more adaptable.
How to define variadic functions in Go:
Variadic functions allow you to pass a variable number of arguments.
package main import "fmt" func sum(nums ...int) int { total := 0 for _, num := range nums { total += num } return total } func main() { result := sum(1, 2, 3, 4, 5) fmt.Println("Sum:", result) }
Passing a slice to a variadic function in Golang:
You can pass a slice to a variadic function using the ...
notation.
package main import "fmt" func sum(nums ...int) int { total := 0 for _, num := range nums { total += num } return total } func main() { numbers := []int{1, 2, 3, 4, 5} result := sum(numbers...) fmt.Println("Sum:", result) }
Variadic parameters in Go examples:
Variadic parameters allow you to accept a variable number of parameters of the same type.
package main import "fmt" func printNames(names ...string) { for _, name := range names { fmt.Println(name) } } func main() { printNames("Alice", "Bob", "Charlie") }
Variadic functions vs slices in Golang:
Variadic functions allow a variable number of arguments, while slices represent a dynamic sequence of elements.
package main import "fmt" // Variadic function func sum(nums ...int) int { total := 0 for _, num := range nums { total += num } return total } // Using a slice func sumSlice(nums []int) int { total := 0 for _, num := range nums { total += num } return total } func main() { result1 := sum(1, 2, 3, 4, 5) fmt.Println("Sum using variadic function:", result1) numbers := []int{1, 2, 3, 4, 5} result2 := sumSlice(numbers) fmt.Println("Sum using slice:", result2) }
Named return values in variadic functions Go:
You can use named return values in variadic functions to improve code readability.
package main import "fmt" func sum(nums ...int) (total int) { for _, num := range nums { total += num } return } func main() { result := sum(1, 2, 3, 4, 5) fmt.Println("Sum:", result) }
Variadic functions and interface{} in Golang:
You can use interface{}
in variadic functions to accept values of any type.
package main import "fmt" func printValues(values ...interface{}) { for _, value := range values { fmt.Println(value) } } func main() { printValues(42, "Hello", 3.14, true) }
Recursive variadic functions in Go:
Variadic functions can also be recursive, allowing for flexible parameter handling.
package main import "fmt" func recursiveSum(first int, rest ...int) int { if len(rest) == 0 { return first } return first + recursiveSum(rest[0], rest[1:]...) } func main() { result := recursiveSum(1, 2, 3, 4, 5) fmt.Println("Sum:", result) }
Error handling in variadic functions Golang:
You can use variadic functions with error handling to handle multiple errors.
package main import ( "errors" "fmt" ) func divide(dividend int, divisors ...int) (float64, error) { if len(divisors) == 0 { return 0, errors.New("cannot divide by zero") } result := float64(dividend) for _, divisor := range divisors { if divisor == 0 { return 0, errors.New("cannot divide by zero") } result /= float64(divisor) } return result, nil } func main() { result, err := divide(10, 2, 5) if err != nil { fmt.Println("Error:", err) } else { fmt.Println("Result:", result) } }