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
What is an Array in Go?
How to initialize an Array with an Array Literal in Go?
Tricks to filter array elements in Go
How to iterate over an Array using for loop?
Copy an array by value and reference into another array
Golang check if array element exists
How to assign and access array element values in Go?
Declaring an Integer or String Array of Five Elements in Go
Initialize values for specific array elements in Go