To get the length of an array in Go, you can use the len
function.
In this example,
numbers
with some values.len
function to get the length of the array and store the returned value in length
variable.len
function returns the length of this array.length
to standard output.package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
length := len(numbers)
fmt.Printf("Length of array is: %d", length)
}
Length of array is: 5
In this example,
names
with some values.len
function to get the length of the array and store the returned value in length
variable.len
function returns the length of this array.length
to standard output.package main
import "fmt"
func main() {
names := []string{"Alice", "Bob", "Charlie", "David", "Eve"}
length := len(names)
fmt.Printf("Length of array is: %d", length)
}
Length of array is: 5
In this tutorial, we learned How to Get Array Length in Go language with well detailed examples.