Go – Get Array Length

To get length of an array in Go, use builtin len() function. Call len() function and pass the array as argument. The function returns an integer representing the length of the array.

The syntax to call len() to find the length of an array arr is

len(arr)

Example

In the following program, we will create an array arr of specific size, and find its length programmatically using len() function.

example.go

package main

import "fmt"

func main() {
	arr := [5]int{1, 2, 3, 4, 5}
	arrLength := len(arr)
	fmt.Println("Length of Array is :", arrLength)
}

Output

Length of Array is : 5
ADVERTISEMENT

Conclusion

In this Golang Tutorial, we learned how to find the length of an Array in Go programming, using len() function, with the help of example program.