Gaaker Go

Golang Variadic functions

A function that can accept a dynamic number of parameters is called a Variadic function In Golang.

The following is the expression.

func plus(numbers ...int)

In the plus function, the value of the variable numbers is of type []int.

The following is a complete code example.

package main

import "fmt"

func plus(numbers ...int) int {
	sum := 0
	for _, number := range numbers {
		sum += number
	}
	return sum
}

func main() {
	println(plus(1))
    println(plus(1,3))
	println(plus(1,2,4))
}

Below is the program output:

1
4
7
< Variadic functions Variadic functions >