ZetCode

Go ellipses

last modified February 8, 2022

Go ellipses tutorial shows how to use the ellipses operator.

The ellipses (...) (or three dots) operator is used in variadic functions, array literals and go commands.

Go variadic function parameters

Variadic functions can be called with any number of trailing arguments.

main.go
package main

import "fmt"

func main() {

    fmt.Println(Sum(1, 2, 3))
    fmt.Println(Sum(1, 2, 3, 4))
    fmt.Println(Sum(1, 2, 3, 4, 5))
}

func Sum(n ...int) int {

    sum := 0

    for _, n := range n {

        sum += n
    }

    return sum
}

In the example, we have the Sum function which can accept any number of integer values.

$ go run main.go 
6
10
15

We can use the three dots when we pass a slice as the argument to the variadic function.

main.go
package main

import "fmt"

func main() {

    vals := []int{1, 2, 3, 4, 5, 6, 7}

    fmt.Println(Sum(vals...))
}

func Sum(n ...int) int {

    sum := 0

    for _, n := range n {

        sum += n
    }

    return sum
}

In the example we pass a slice of integers to the Sum function.

Infering array length

We can infer the array length when using array literals with the ellipses ... operator.

main.go
package main

import "fmt"

func main() {

    vals := [...]int{ 1, 2, 3, 4, 5, 6 }
    fmt.Println(vals)
}

In the code example, we use the ... in the array declaration. This tells Go to infer the length of the array from the provided array literal.

$ go run main.go
[1 2 3 4 5 6]

Wildcards

The ellipses can be used as wildcars in go commands.

$ go list ...
archive/tar
archive/zip
bufio
bytes
compress/bzip2
compress/flate
compress/gzip
compress/lzw
compress/zlib
container/heap
container/list
container/ring
context
crypto
crypto/aes
crypto/cipher
...

The command lists all packages recursively, including the packages from the standard library in the current directory.

In this tutorial, we have covered the ellipses (three dots) operator in Golang.

List all Go tutorials.