📖 Scenario: Imagine you are managing a list of fruits in a basket. You want to add more fruits to this basket as you go shopping.
🎯 Goal: You will create a slice of fruits, add more fruits to it using the append function, and then print the final list of fruits.
📋 What You'll Learn
Create a slice of strings called fruits with initial fruits
Create a slice of strings called newFruits with fruits to add
Use append to add newFruits to fruits
Print the final fruits slice
💡 Why This Matters
🌍 Real World
Managing lists of items dynamically is common in many programs, like shopping carts, playlists, or task lists.
💼 Career
Understanding slices and how to append to them is essential for Go developers working on data processing, web servers, or any application that handles collections of data.
Progress0 / 4 steps
1
Create the initial slice of fruits
Create a slice of strings called fruits with these exact values: "apple", "banana", "cherry".
Go
Hint
Use fruits := []string{"apple", "banana", "cherry"} to create the slice.
2
Create a slice of new fruits to add
Create a slice of strings called newFruits with these exact values: "date", "elderberry".
Go
Hint
Use newFruits := []string{"date", "elderberry"} to create the slice.
3
Append the new fruits to the original slice
Use the append function to add all elements of newFruits to fruits. Assign the result back to fruits.
Go
Hint
Use fruits = append(fruits, newFruits...) to add all new fruits.
4
Print the final fruits slice
Write a fmt.Println statement to print the fruits slice.
Go
Hint
Use fmt.Println(fruits) to display the slice.
Practice
(1/5)
1. What does the append function do in Go when used with slices?
easy
A. It adds new elements to the end of a slice, returning the updated slice.
B. It removes elements from the beginning of a slice.
C. It sorts the elements of a slice in ascending order.
D. It copies elements from one slice to another without changing size.
Solution
Step 1: Understand the purpose of append
The append function is designed to add elements to the end of a slice in Go.
Step 2: Recognize the return value
Append returns a new slice that includes the original elements plus the added ones.
Final Answer:
It adds new elements to the end of a slice, returning the updated slice. -> Option A
Quick Check:
append adds elements to slice end = A [OK]
Hint: Append adds elements at the end and returns the new slice [OK]
Common Mistakes:
Thinking append removes elements
Assuming append sorts the slice
Not realizing append returns a new slice
2. Which of the following is the correct way to append the integer 5 to a slice named nums?
easy
A. nums.append(5)
B. append(nums, 5)
C. nums = append(nums, 5)
D. nums += 5
Solution
Step 1: Recall append syntax in Go
Append returns a new slice, so you must assign it back to the original slice variable.
Step 2: Check each option
nums = append(nums, 5) correctly assigns the result of append back to nums. Options A, B, and D don't work correctly.
Final Answer:
nums = append(nums, 5) -> Option C
Quick Check:
Assign append result back to slice = C [OK]
Hint: Always assign append result back to your slice variable [OK]