¿What does the 3 dots do?

package main

import "fmt"

func main() {
  var array  = []int{1, 2, 3, 4, 5}
  fmt.Println(array)
  array  = append(array [:1], array [2:]...) // The 3 dots.
  fmt.Println(array)
}

array is a slice of int so the parameters in the append function must be int.
the first parameter array[:1] = 1 so it is OK because is an int
next parameter array [2:] = [3 4 5] so the three dots converts this slice to 3,4,5
For the compileer the final statement is append(1, 3, 4, 5)

The ... parameter syntax makes a variadic parameter. It will accept zero or more arguments, and reference them as a slice.

2 Likes

Thanks for explaining.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.