📖 Scenario: You are managing a list of tasks to do today. Sometimes, you finish the last task and want to remove it from your list.
🎯 Goal: Build a simple Go program that creates a list of tasks, sets up a count of tasks, removes the last task from the list, and then prints the updated list.
📋 What You'll Learn
Create a slice of strings called tasks with exactly these values: "Laundry", "Dishes", "Homework", "Shopping"
Create an integer variable called count that stores the number of tasks in the tasks slice
Remove the last task from the tasks slice by slicing it
Print the updated tasks slice
💡 Why This Matters
🌍 Real World
Managing lists of tasks, items, or data where you often add or remove the last element, like a to-do list or recent files list.
💼 Career
Understanding how to manipulate slices is essential for Go developers working on applications that handle dynamic lists, such as task managers, inventory systems, or user activity logs.
Progress0 / 4 steps
1
Create the tasks list
Create a slice of strings called tasks with these exact values: "Laundry", "Dishes", "Homework", "Shopping"
DSA Go
Hint
Use tasks := []string{} to create the list with the exact tasks inside.
2
Count the number of tasks
Create an integer variable called count that stores the number of tasks in the tasks slice using the len function
DSA Go
Hint
Use count := len(tasks) to get the number of tasks.
3
Remove the last task
Remove the last task from the tasks slice by slicing it to keep only the first count - 1 elements
DSA Go
Hint
Use slicing like tasks = tasks[:count-1] to remove the last task.
4
Print the updated tasks list
Print the updated tasks slice using fmt.Println(tasks)
DSA Go
Hint
Use fmt.Println(tasks) to show the updated list.
Practice
(1/5)
1. What happens when you delete an element from the end of an array in Go?
easy
A. The last element is removed and the array size decreases by one.
B. The first element is removed and the array size decreases by one.
C. The array size stays the same but the last element becomes zero.
D. The array is completely cleared and becomes empty.
Solution
Step 1: Understand array deletion at end
Deleting at the end means removing the last element only, not the first or all.
Step 2: Effect on array size
After removal, the array size decreases by one because one element is gone.
Final Answer:
The last element is removed and the array size decreases by one. -> Option A
Quick Check:
Deleting end element reduces size by one [OK]
Hint: Deleting at end removes last element and shrinks array [OK]
Common Mistakes:
Thinking first element is removed
Assuming array size stays same
Believing array clears completely
2. Which of the following Go code snippets correctly deletes the last element from a slice named arr?
easy
A. arr = arr[len(arr):]
B. arr = arr[:len(arr)-1]
C. arr = arr[1:]
D. arr = arr[:len(arr)+1]
Solution
Step 1: Understand slice slicing syntax
To remove last element, slice up to length minus one: arr[:len(arr)-1].
Step 2: Check other options
arr[len(arr):] results in empty slice, arr[1:] removes first element, arr[:len(arr)+1] causes runtime error.
Final Answer:
arr = arr[:len(arr)-1] -> Option B
Quick Check:
Slice up to len-1 removes last element [OK]
Hint: Use arr[:len(arr)-1] to drop last element [OK]