Golang: Variadic Function

By Xah Lee. Date: .

For unspecified number of argument (aka variadic function) , use this syntax, example:

func f(x ...type)

Example:
func f(args ...int)

The arguments are received as a slice type in the function body.

package main

import "fmt"

// function with unspecified number of args
func ff(x ...int) []int {
	return x
}

func main() {
	fmt.Printf("%#v\n", ff(3, 7))
	// []int{3, 7}
}

golang function