Menu
Coddy logo textTech

Iterating Over Arrays

Part of the Fundamentals section of Coddy's GO journey — lesson 72 of 109.

You can iterate through arrays using a for loop with an index.

Create an array of weekdays:

weekdays := [5]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}

Iterate through the array using a for loop:

for i := 0; i < len(weekdays); i++ {
    fmt.Println(i, weekdays[i])
}

This displays each element with its index:

0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
challenge icon

Challenge

Beginner

In this challenge, you'll practice iterating over an array of fruits using a for loop. The array is already defined for you. Your task is to complete the for loop to print each fruit in the array on a new line.

Cheat sheet

You can iterate through arrays using a for loop with an index:

weekdays := [5]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}

for i := 0; i < len(weekdays); i++ {
    fmt.Println(i, weekdays[i])
}

This displays each element with its index.

Try it yourself

package main

import "fmt"

func main() {
	// Array of fruits
	fruits := [5]string{"Apple", "Banana", "Orange", "Grape", "Mango"}
	
	// TODO: Complete the for loop to print each fruit in the array
	for i := 0; i < len(fruits); i++ {
		// Add your code here to print each fruit
		
	}
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals