Arrays help us store many items together in one place. This makes it easy to keep and use a list of things.
When you want to keep a list of student names in a class.
When you need to store daily temperatures for a week.
When you want to save scores of players in a game.
When you want to remember multiple phone numbers.
When you want to process a group of items one by one.
Syntax
Go
package main
import"fmt"
func main() {
var numbers [5]int// array of 5 integers
numbers[0] = 10// set first item
numbers[1] = 20// set second item
fmt.Println(numbers) // print the whole array
}
An array has a fixed size, set when you create it.
You access items by their position, starting at 0.
Examples
This creates an array of 3 integers, all set to zero by default.
Go
var emptyArray [3]int
fmt.Println(emptyArray)
An array with only one item can still be useful to hold a single value in a list form.
Go
var singleElementArray [1]string
singleElementArray[0] = "hello"
fmt.Println(singleElementArray)
This creates and fills an array with 3 fruit names right away.
Go
var fruits = [3]string{"apple", "banana", "cherry"}
fmt.Println(fruits)
Sample Program
This program shows how to create an array, store values, and get values by position.
Go
package main
import"fmt"
func main() {
// Create an array to store 4 temperatures
var temperatures [4]int// Set values for each day
temperatures[0] = 70
temperatures[1] = 72
temperatures[2] = 68
temperatures[3] = 65// Print all temperatures
fmt.Println("Temperatures for 4 days:", temperatures)
// Access one day's temperature
fmt.Println("Temperature on day 2:", temperatures[1])
}
OutputSuccess
Important Notes
Arrays have a fixed size, so you cannot add more items than the size.
Accessing an array item by index is very fast (constant time).
Common mistake: Trying to access an index outside the array size causes an error.
Use arrays when you know the number of items in advance and it won't change.
Summary
Arrays store multiple items together in one variable.
They have a fixed size set when created.
Use arrays to keep lists of things you want to access by position.
Practice
(1/5)
1. Why do we need arrays in Go programming?
easy
A. To store multiple values of the same type together in one variable
B. To store only one value at a time
C. To perform mathematical calculations faster
D. To create functions that run automatically
Solution
Step 1: Understand the purpose of arrays
Arrays allow storing many values of the same type in one place, making it easier to manage related data.
Step 2: Compare options with array purpose
Only To store multiple values of the same type together in one variable correctly describes storing multiple values together; others describe unrelated concepts.
Final Answer:
To store multiple values of the same type together in one variable -> Option A
Quick Check:
Arrays store multiple values = A [OK]
Hint: Arrays hold many same-type items in one variable [OK]
Common Mistakes:
Thinking arrays store different types together
Confusing arrays with single variables
Assuming arrays speed up math automatically
2. Which of the following is the correct way to declare an array of 3 integers in Go?
easy
A. var arr [3]int
B. arr := []int{3}
C. var arr int[3]
D. arr = array(3)int
Solution
Step 1: Recall Go array declaration syntax
In Go, arrays are declared with var name [size]type, for example var arr [3]int.
Step 2: Check each option
var arr [3]int matches correct syntax. arr := []int{3} is a slice, not an array. Options A and D are invalid syntax.
Final Answer:
var arr [3]int -> Option A
Quick Check:
Array declaration uses var name [size]type = C [OK]
Hint: Remember: var name [size]type declares an array [OK]