Operator and syntax

Go ellipsis syntax

Three dots declare a variadic parameter, expand a slice for a compatible variadic call, or infer array length from a literal. These grammar contexts have different meanings. Example output: '3'.

Formal Syntax & Structure

go syntaxgo
func f(values ...T); f(slice...); [...]T{...}

Working Example

Example Snippetgo
package main
import (  "fmt")
func sum(xs ...int) int {  n := 0  for _, x := range xs {    n += x  }  return n}func main() {  a := [...]int{1, 2}  fmt.Println(sum(a[:]...))}
Go Reference