📖 Scenario: You are working on a program that needs to know how many items are in a list of fruits. This is common when you want to count things in real life, like how many apples you have in a basket.
🎯 Goal: Build a Go program that creates an array of fruits, then finds and prints the length of that array.
📋 What You'll Learn
Create an array called fruits with exactly these values: "apple", "banana", "cherry", "date"
Create a variable called length that stores the length of the fruits array
Print the value of length to show how many fruits are in the array
💡 Why This Matters
🌍 Real World
Counting items in a list is common in many programs, like counting products in a shopping cart or names in a guest list.
💼 Career
Knowing how to find the length of arrays or lists is a basic skill for programming jobs that involve data processing or user input.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact string values: "apple", "banana", "cherry", "date"
Go
Hint
Use square brackets with the number 4 to create an array of 4 strings.
2
Create the length variable
Create a variable called length that stores the length of the fruits array using the built-in len function
Go
Hint
Use len(fruits) to get the number of items in the array.
3
Print the length
Use fmt.Println to print the value of the length variable
Go
Hint
Use fmt.Println(length) to show the number of fruits.
4
Run the program to see the output
Run the program and observe the output. It should print the number 4 because there are four fruits in the array.
Go
Hint
The program should print 4 because there are four items in the array.
Practice
(1/5)
1. What does the len() function return when used with an array in Go?
easy
A. The memory size of the array in bytes
B. The number of elements in the array
C. The capacity of the array's underlying slice
D. The last element of the array
Solution
Step 1: Understand the purpose of len() in Go
The len() function returns the count of elements in arrays, slices, maps, and strings.
Step 2: Apply len() to an array
When used on an array, len() returns the fixed number of elements it holds.
Final Answer:
The number of elements in the array -> Option B
Quick Check:
len(array) = number of elements [OK]
Hint: len() always gives count of elements in an array [OK]
Common Mistakes:
Confusing len() with capacity
Thinking len() returns memory size
Assuming len() returns last element
2. Which of the following is the correct way to get the length of an array named arr in Go?
easy
A. len(arr)
B. arr.length
C. size(arr)
D. length(arr)
Solution
Step 1: Recall Go syntax for length
In Go, the built-in function len() is used to get the length of arrays and slices.
Step 2: Check each option
Only len(arr) is valid syntax; others are invalid or do not exist in Go.
Final Answer:
len(arr) -> Option A
Quick Check:
Use len() function for array length [OK]
Hint: Use len(array) to get length, not dot or other functions [OK]
Common Mistakes:
Using dot notation like arr.length
Using non-existent functions like length() or size()