OneBite.Dev - Coding blog in a bite size

find the length of an array in Go

Code snippet on how to find the length of an array in Go

package main

import "fmt"

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

This code starts out by importing the package “fmt” in order to use the fmt print function for this example. The first line inside of the main function creates an array of type int with 5 elements. The second line assigns the length of the array to a variable called “arrLength” and then the fmt print function is used to print the length of the array to the terminal. The output of this code is “The length of the array is: 5”.

go