Function semantics

Variadic parameters

Only the final parameter may be variadic. Within the function it has slice type; a call with no variadic arguments supplies a nil slice. Passing separate arguments creates a new slice for those arguments. Example output: '0 9'.

Formal Syntax & Structure

go syntaxgo
func sum(values ...int) int

Working Example

Example Snippetgo
package main
import "fmt"
func sum(values ...int) int {  n := 0  for _, v := range values {    n += v  }  return n}func main() {  fmt.Println(sum(), sum(2, 3, 4))}
Go Reference