📖 Scenario: You are managing a list of fruits in a grocery store inventory system. You want to understand how slices work in Go, especially their length and capacity, to handle the inventory efficiently.
🎯 Goal: Learn how to create a slice, check its length and capacity, and understand how these properties change when slicing the slice.
📋 What You'll Learn
Create a slice of strings with exact fruits
Create a new slice from the original slice
Print the length and capacity of both slices
💡 Why This Matters
🌍 Real World
Slices are used in Go programs to manage collections of data efficiently, such as lists of items in inventory or user data.
💼 Career
Understanding slice length and capacity is essential for Go developers to write efficient and bug-free code when handling dynamic data.
Progress0 / 4 steps
1
Create the initial slice
Create a slice of strings called fruits with these exact values: "apple", "banana", "cherry", "date", "elderberry".
Go
Hint
Use []string{} to create a slice with the exact fruit names inside.
2
Create a new slice from the original
Create a new slice called citrus from fruits that contains only the elements from index 1 to 3 (inclusive start, exclusive end).
Go
Hint
Use slicing syntax fruits[1:4] to get elements from index 1 up to but not including 4.
3
Check length and capacity
Create two variables called fruitsLen and fruitsCap to store the length and capacity of fruits. Also create citrusLen and citrusCap for the length and capacity of citrus.
Go
Hint
Use len(slice) for length and cap(slice) for capacity.
4
Print the lengths and capacities
Print the length and capacity of fruits and citrus using fmt.Println with the exact format: "fruits length: " followed by fruitsLen, "fruits capacity: " followed by fruitsCap, "citrus length: " followed by citrusLen, "citrus capacity: " followed by citrusCap.
Go
Hint
Use fmt.Println four times with the exact text and variables.
Practice
(1/5)
1. In Go, what does the len() function return when used on a slice?
easy
A. The maximum number of elements the slice can hold
B. The total memory size of the slice in bytes
C. The index of the last element in the slice
D. The number of elements currently in the slice
Solution
Step 1: Understand the purpose of len() on slices
The len() function returns how many elements are currently stored in the slice.
Step 2: Differentiate from capacity
Capacity is about how many elements the slice can hold before resizing, not the current count.
Final Answer:
The number of elements currently in the slice -> Option D
Quick Check:
len(slice) = current element count [OK]
Hint: Remember: len = current items, cap = max before grow [OK]
Common Mistakes:
Confusing length with capacity
Thinking len returns memory size
Assuming len gives last index
2. Which of the following is the correct way to create a slice with length 3 and capacity 5 in Go?
easy
A. make([]int, 5, 3)
B. make([]int, 3, 5)
C. []int{3, 5}
D. make([]int, 3)
Solution
Step 1: Recall the make function signature for slices
make([]T, length, capacity) creates a slice with given length and capacity.
Step 2: Match length and capacity values
To get length 3 and capacity 5, use make([]int, 3, 5).
Final Answer:
make([]int, 3, 5) -> Option B
Quick Check:
make([]int, length, capacity) [OK]
Hint: make([]T, len, cap) sets length then capacity [OK]