📖 Scenario: You are organizing a small fruit basket for a friend. You want to keep track of the fruits you have using a list-like structure called a slice in Go.
🎯 Goal: Create a slice of fruits, add a few fruit names, and then display the list.
📋 What You'll Learn
Create a slice of strings called fruits with the exact fruits: "Apple", "Banana", "Cherry"
Create a variable called extraFruit and set it to "Mango"
Add extraFruit to the fruits slice using the append function
Print the fruits slice to show all fruits
💡 Why This Matters
🌍 Real World
Slices are used in Go to store lists of items like names, numbers, or objects. This is common in apps that manage collections such as shopping lists, user data, or inventory.
💼 Career
Understanding slices is essential for Go programming jobs because they are a fundamental way to handle groups of data efficiently.
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{} to create a slice with the fruit names inside curly braces.
2
Add an extra fruit variable
Create a variable called extraFruit and set it to the string "Mango"
Go
Hint
Use extraFruit := "Mango" to create the variable.
3
Add the extra fruit to the slice
Use the append function to add extraFruit to the fruits slice and update fruits with the result
Go
Hint
Use fruits = append(fruits, extraFruit) to add the new fruit.
4
Print the fruits slice
Write a fmt.Println statement to print the fruits slice
Go
Hint
Use fmt.Println(fruits) to display the slice contents.
Practice
(1/5)
1. What is a slice in Go programming?
easy
A. A flexible, dynamically-sized list of elements.
B. A fixed-size array that cannot change length.
C. A function that slices strings.
D. A type of map with keys and values.
Solution
Step 1: Understand slice definition
A slice in Go is a data structure that holds a list of elements and can grow or shrink in size.
Step 2: Compare with other types
Unlike arrays, slices are dynamic and flexible, unlike maps or functions.
Final Answer:
A flexible, dynamically-sized list of elements. -> Option A
Quick Check:
Slice = dynamic list [OK]
Hint: Remember: slices can grow, arrays cannot [OK]
Common Mistakes:
Confusing slices with fixed-size arrays
Thinking slices are functions
Mixing slices with maps
2. Which of the following is the correct way to create an empty slice of integers in Go?
easy
A. var s []int
B. s := []int{}
C. s := make([]int, 0)
D. All of the above
Solution
Step 1: Review slice creation methods
In Go, you can create an empty slice by declaring a nil slice, using a slice literal with no elements, or using make with length zero.
Step 2: Confirm all options are valid
All three options create an empty slice of integers correctly.
Final Answer:
All of the above -> Option D
Quick Check:
Empty slice creation = D [OK]
Hint: Empty slices can be made in multiple valid ways [OK]
Common Mistakes:
Using make with negative length
Confusing array and slice syntax
Forgetting to initialize slice before use
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
s := []int{1, 2, 3}
s = append(s, 4)
fmt.Println(s)
}
medium
A. [1 2 3]
B. [1 2 3 4]
C. [4]
D. Compilation error
Solution
Step 1: Understand initial slice and append
The slice s starts with elements 1, 2, 3. The append function adds 4 to the end.
Step 2: Print the updated slice
After append, s contains [1 2 3 4], which is printed.
Final Answer:
[1 2 3 4] -> Option B
Quick Check:
append adds element to slice [OK]
Hint: append adds elements to the end of a slice [OK]
Common Mistakes:
Expecting append to replace elements
Thinking append returns a new slice without assignment
Confusing slice with array syntax
4. Identify the error in this Go code snippet:
package main
func main() {
s := make([]int, 3)
s[3] = 10
}
medium
A. Slice s is nil
B. Syntax error in make function
C. Index out of range error at s[3]
D. No error, code runs fine
Solution
Step 1: Analyze slice length and indexing
make([]int, 3) creates a slice with length 3, valid indices are 0, 1, 2.
Step 2: Check index used in assignment
Accessing s[3] is out of range and causes a runtime panic.
Final Answer:
Index out of range error at s[3] -> Option C
Quick Check:
Slice indices start at 0, max index = length-1 [OK]
Hint: Slice indices go from 0 to length-1 only [OK]
Common Mistakes:
Using index equal to slice length
Confusing slice length and capacity
Assuming make creates larger slice than specified
5. Given the code below, what will be the length and capacity of slice s after execution?
package main
import "fmt"
func main() {
s := make([]int, 2, 5)
s = append(s, 3, 4, 5)
fmt.Println(len(s), cap(s))
}
hard
A. 5 5
B. 5 10
C. 7 10
D. 3 5
Solution
Step 1: Understand make parameters
make([]int, 2, 5) creates a slice with length 2 and capacity 5.
Step 2: Append three elements
Appending 3 elements increases length from 2 to 5, capacity remains 5 as it is enough.
Final Answer:
5 5 -> Option A
Quick Check:
Length grows with append, capacity stays if enough [OK]
Hint: Capacity grows only if append exceeds current capacity [OK]
Common Mistakes:
Assuming capacity doubles automatically
Confusing length and capacity
Forgetting append increases length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.