0
0
Goprogramming~15 mins

For loop basics in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
For loop basics
๐Ÿ“– Scenario: You work in a small grocery store. You have a list of fruit names that you want to check one by one.
๐ŸŽฏ Goal: You will write a Go program that uses a for loop to go through a list of fruits and print each fruit's name.
๐Ÿ“‹ What You'll Learn
Create a slice of strings with exact fruit names
Create a variable to count fruits
Use a for loop with index and value to iterate over the slice
Print each fruit name inside the loop
Print the total number of fruits after the loop
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Loops are used to process lists of items like products, names, or data entries one by one.
๐Ÿ’ผ Career
Understanding loops is essential for any programming job because they help automate repetitive tasks efficiently.
Progress0 / 4 steps
1
Create a slice of fruits
Create a slice of strings called fruits with these exact values: "apple", "banana", "cherry", "date", "elderberry".
Go
Need a hint?

Use fruits := []string{...} to create the slice with the exact fruit names.

2
Create a counter variable
Create an integer variable called count and set it to 0 to count the fruits.
Go
Need a hint?

Use count := 0 to create the counter variable.

3
Use a for loop to print fruits
Use a for loop with variables index and fruit to iterate over fruits. Inside the loop, print the fruit name using fmt.Println(fruit) and increase count by 1.
Go
Need a hint?

Use for index, fruit := range fruits to loop. Use fmt.Println(fruit) to print each fruit. Increase count by 1 inside the loop.

4
Print the total count
After the loop, print the total number of fruits using fmt.Println("Total fruits:", count).
Go
Need a hint?

Use fmt.Println("Total fruits:", count) to show the total number of fruits after the loop.