0
0
Goprogramming~15 mins

Slice creation in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Slice creation
📖 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.