¿What does the 3 dots do?


(Illud) #1
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)
}

(Yamil Bracho) #2

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.


(Illud) #3

Thanks for explaining.