Iterating Over Slices
Part of the Fundamentals section of Coddy's GO journey — lesson 81 of 109.
To iterate over a slice in Go, you can use a for loop with the range keyword.
Create a slice of fruits:
fruits := []string{"Apple", "Banana", "Cherry"}
fmt.Println("Fruits:", fruits)Iterate through the slice using range:
for index, value := range fruits {
fmt.Printf("Index: %d, Value: %s\n", index, value)
}Result:
Fruits: [Apple Banana Cherry]
Index: 0, Value: Apple
Index: 1, Value: Banana
Index: 2, Value: CherryIf you only need the values, use the blank identifier (_):
for _, fruit := range fruits {
fmt.Println(fruit)
}Challenge
EasyIn this challenge, you'll practice iterating over a slice of fruits using the range keyword. Your task is to loop through the slice and print each fruit with its position in the list.
The slice is already defined for you. You need to complete the for loop using range to iterate through the slice and print each fruit with its position (starting from 1).
Cheat sheet
To iterate over a slice in Go, use a for loop with the range keyword:
fruits := []string{"Apple", "Banana", "Cherry"}
for index, value := range fruits {
fmt.Printf("Index: %d, Value: %s\n", index, value)
}If you only need the values, use the blank identifier (_):
for _, fruit := range fruits {
fmt.Println(fruit)
}Try it yourself
package main
import "fmt"
func main() {
// A slice of fruits
fruits := []string{"Apple", "Banana", "Cherry", "Dragon fruit", "Elderberry"}
// TODO: Complete the for loop using range to iterate through the fruits slice
// Print each fruit with its position like: "1. Apple"
for // Add your range loop here {
// Add your code here to print each fruit with its position
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions