go length of array code example

Example 1: golang size of slice

// Create an exmaple array
    array := []int{1, 2, 3, 4, 5}
// Print number of items
    fmt.Println("First Length:", len(array))

Example 2: how to get the length of an array in go

array:=[]int {1,2,3,4}
len(array)

Example 3: go arrays

var a [10]int // declare an int array with length 10. Array length is part of the type!
a[3] = 42     // set elements
i := a[3]     // read elements

// declare and initialize
var a = [2]int{1, 2}
a := [2]int{1, 2} //shorthand
a := [...]int{1, 2} // elipsis -> Compiler figures out array length

Tags:

Go Example