Initializing an Array with ellipses in Go


When we use ... instead of specifying the length. The compiler can identify the length of an array, based on the elements specified in the array declaration.

Example

package main

import (
	"fmt"
	"reflect"
)

func main() {
	x := [...]int{10, 20, 30}

	fmt.Println(reflect.ValueOf(x).Kind())
	fmt.Println(len(x))
}

Output

array
3
Most Helpful This Week