What if you could stop juggling many separate values and keep them all neatly in one place?
Why Array declaration in Go? - Purpose & Use Cases
Imagine you want to keep track of your daily expenses for a week. You try to write each amount on a separate piece of paper and then add them up manually.
This manual way is slow and confusing. You might lose some papers or forget some amounts. Adding them up by hand can cause mistakes, especially if you have many days to track.
Using an array lets you store all your daily expenses in one place. You can easily access, update, and calculate totals without losing track or making errors.
expense1 := 10 expense2 := 15 expense3 := 20 // and so on...
var expenses [7]int expenses[0] = 10 expenses[1] = 15 expenses[2] = 20
Arrays let you organize many related values neatly, making your programs simpler and less error-prone.
Think of a row of mailboxes where each box holds letters for a day. An array is like that row, keeping all your daily data in order.
Arrays store multiple values of the same type together.
They help keep data organized and easy to access.
Using arrays reduces mistakes compared to handling many separate variables.
Practice
var arr [3]intSolution
Step 1: Understand the syntax
This declares a fixed-size array namedvar arr [3]intarrwith 3 elements, each of typeint.Step 2: Recognize default initialization
In Go, arrays are initialized with zero values for their type, so all 3 integers start as 0.Final Answer:
An array of 3 integers initialized with zero values -> Option BQuick Check:
Array declaration = fixed size + type + zero values [OK]
- Confusing array with slice declaration
- Thinking it creates a pointer or map
- Assuming uninitialized values are random
Solution
Step 1: Check Go array declaration syntax
Arrays are declared withvar name [size]type. Sovar arr [4]stringis correct.Step 2: Analyze other options
var arr []string{4} uses slice syntax incorrectly. arr := [4]string{} is valid short declaration syntax in Go. var arr string[4] reverses size and type, which is invalid.Final Answer:
var arr [4]string -> Option AQuick Check:
Array syntax = var name [size]type [OK]
- Using slice syntax instead of array syntax
- Placing size after type
- Missing var keyword or wrong initialization
package main
import "fmt"
func main() {
var nums = [3]int{10, 20, 30}
fmt.Println(nums[1])
}Solution
Step 1: Understand array initialization
The arraynumshas 3 integers: 10 at index 0, 20 at index 1, and 30 at index 2.Step 2: Access element at index 1
Printingnums[1]outputs the second element, which is 20.Final Answer:
20 -> Option DQuick Check:
Array index starts at 0, nums[1] = 20 [OK]
- Confusing index 1 with index 0
- Expecting output 10 or 30 instead of 20
- Thinking array is zero-indexed but printing wrong element
var arr [3]int = {1, 2, 3}Solution
Step 1: Review array initialization syntax
In Go, when initializing an array with values, the correct syntax isvar arr = [3]int{1, 2, 3}orarr := [3]int{1, 2, 3}.Step 2: Identify the error
The code usesvar arr [3]int = {1, 2, 3}which misses the array type on the right side before the braces. This causes a syntax error.Final Answer:
Array initialization syntax is incorrect -> Option AQuick Check:
Array init needs type before braces [OK]
- Omitting array type on right side during initialization
- Using wrong braces or brackets
- Trying to assign without proper syntax
Solution
Step 1: Understand 2D array declaration
A 2D array with 2 rows and 3 columns is declared as[2][3]int. Both dimensions must be specified.Step 2: Check initialization syntax
matrix := [2][3]int{{1,2,3},{4,5,6}} uses short declaration with:=and correctly initializes the 2D array with two rows of three integers each.Step 3: Analyze other options
var matrix [2][3]int = [2][3]int{{1,2,3},{4,5,6}} is correct syntax but uses explicitvarwith type on both sides (valid but less common). var matrix [][]int = {{1,2,3},{4,5,6}} is a slice of slices, not an array. var matrix [3][2]int = {{1,2},{3,4},{5,6}} has dimensions reversed (3 rows, 2 columns).Final Answer:
matrix := [2][3]int{{1,2,3},{4,5,6}} -> Option CQuick Check:
2D array = [rows][cols]type with nested braces [OK]
- Confusing slices with arrays
- Swapping row and column sizes
- Missing nested braces for 2D arrays
Ready to prove it?
Pass the test and this pattern moves unknown
