Menu
Coddy logo textTech

Array Length with `len`

Part of the Fundamentals section of Coddy's GO journey — lesson 71 of 109.

The len() function returns the length (number of elements) of an array.

Create an array of colors:

colors := [4]string{"Red", "Blue", "Green", "Yellow"}

Get the array length using len():

arrayLength := len(colors)
fmt.Println("The array has", arrayLength, "elements")

This displays:

The array has 4 elements
challenge icon

Challenge

Beginner
In this challenge, you'll practice using the len() function to find the length of an array.

We've provided an array of favorite fruits. Your task is to find the length of this array using the len() function and store it in the numberOfFruits variable.

Cheat sheet

The len() function returns the length (number of elements) of an array:

colors := [4]string{"Red", "Blue", "Green", "Yellow"}
arrayLength := len(colors)
fmt.Println("The array has", arrayLength, "elements")

Output:

The array has 4 elements

Try it yourself

package main

import "fmt"

func main() {
	// This is our array of favorite fruits
	favoriteFruits := [5]string{"Apple", "Banana", "Orange", "Mango", "Strawberry"}
	
	// TODO: Use the len() function to find the length of the favoriteFruits array
	// and store it in the numberOfFruits variable
	var numberOfFruits int
	
	// This will print the number of fruits in the array
	fmt.Printf("There are %d fruits in the array\n", numberOfFruits)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals