0
0
Goprogramming~15 mins

Accessing array elements in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing array elements
📖 Scenario: You are organizing a small fruit basket. You want to keep track of the fruits you have and check specific fruits by their position in the basket.
🎯 Goal: Learn how to create an array of fruits and access specific fruits by their position (index) in the array.
📋 What You'll Learn
Create an array of fruits with exact values
Create a variable to hold the index of the fruit to access
Access the fruit at the given index from the array
Print the accessed fruit
💡 Why This Matters
🌍 Real World
Arrays are used to store collections of items like fruits, names, or numbers where you want to access items by their position.
💼 Career
Understanding how to access array elements is fundamental for programming tasks such as data processing, user input handling, and working with lists in software development.
Progress0 / 4 steps
1
Create an array of fruits
Create an array called fruits with these exact values: "Apple", "Banana", "Cherry", "Date", "Elderberry".
Go
Hint

Use the syntax varName := [size]type{values} to create an array.

2
Create an index variable
Create an integer variable called index and set it to 2 to select the third fruit in the array.
Go
Hint

Use variableName := value to create and set a variable.

3
Access the fruit at the given index
Create a variable called selectedFruit and set it to the fruit at position index in the fruits array.
Go
Hint

Use arrayName[index] to access an element at a specific position.

4
Print the selected fruit
Use fmt.Println to print the value of selectedFruit.
Go
Hint

Use fmt.Println(variableName) to print the value to the screen.