0
0
Goprogramming~15 mins

Array limitations in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Array limitations
📖 Scenario: You are working with a fixed-size array in Go to store daily temperatures for a week. You want to understand how arrays have fixed sizes and what happens when you try to access or assign values outside their limits.
🎯 Goal: Build a simple Go program that creates an array of 7 integers representing temperatures, sets a configuration variable for the maximum valid index, tries to assign values within the array limits, and then prints the array.
📋 What You'll Learn
Create an array called temps with exactly 7 integers initialized to zero
Create an integer variable called maxIndex and set it to 6
Use a for loop with variable i from 0 to maxIndex to assign temps[i] = i * 5
Print the temps array using fmt.Println
💡 Why This Matters
🌍 Real World
Arrays are used in programs where you know the exact number of items in advance, like storing fixed daily data such as temperatures or sensor readings.
💼 Career
Understanding array limitations helps avoid runtime errors and is fundamental for working with data structures in Go programming jobs.
Progress0 / 4 steps
1
Create the array
Create an array called temps of type [7]int initialized with zeros.
Go
Hint

Use var temps [7]int to create an array of 7 integers initialized to zero.

2
Add maxIndex variable
Add an integer variable called maxIndex and set it to 6.
Go
Hint

Declare maxIndex as var maxIndex int = 6.

3
Assign values using a for loop
Use a for loop with variable i from 0 to maxIndex inclusive to assign temps[i] = i * 5.
Go
Hint

Use a for loop from 0 to maxIndex and assign temps[i] = i * 5 inside the loop.

4
Print the array
Print the temps array using fmt.Println(temps).
Go
Hint

Use fmt.Println(temps) to print the array.