Bird
Raised Fist0
DSA Goprogramming

Array Declaration and Initialization in DSA Go

Choose your learning style10 modes available
Mental Model
An array is a fixed-size collection of items stored one after another in memory. Declaring and initializing means creating this collection and putting values inside it.
Analogy: Think of an egg carton with fixed slots. Declaring the array is like getting the empty carton, and initializing is like placing eggs in each slot.
[0] [1] [2] [3] [4]
  ↑   ↑   ↑   ↑   ↑
  0   0   0   0   0
Dry Run Walkthrough
Input: Declare an array of 5 integers and initialize with values 10, 20, 30, 40, 50
Goal: Create an array with 5 slots and fill each slot with the given numbers
Step 1: Declare an array of size 5 with default zero values
[0] [1] [2] [3] [4]
  0   0   0   0   0
Why: We need a container with 5 slots to hold integers
Step 2: Assign 10 to slot 0
[0] [1] [2] [3] [4]
 10   0   0   0   0
Why: Put the first value in the first slot
Step 3: Assign 20 to slot 1
[0] [1] [2] [3] [4]
 10  20   0   0   0
Why: Put the second value in the second slot
Step 4: Assign 30 to slot 2
[0] [1] [2] [3] [4]
 10  20  30   0   0
Why: Put the third value in the third slot
Step 5: Assign 40 to slot 3
[0] [1] [2] [3] [4]
 10  20  30  40   0
Why: Put the fourth value in the fourth slot
Step 6: Assign 50 to slot 4
[0] [1] [2] [3] [4]
 10  20  30  40  50
Why: Put the fifth value in the fifth slot
Result:
[0] [1] [2] [3] [4]
 10  20  30  40  50
Annotated Code
DSA Go
package main

import "fmt"

func main() {
    // Declare an array of 5 integers
    var arr [5]int

    // Initialize each element
    arr[0] = 10 // assign 10 to first slot
    arr[1] = 20 // assign 20 to second slot
    arr[2] = 30 // assign 30 to third slot
    arr[3] = 40 // assign 40 to fourth slot
    arr[4] = 50 // assign 50 to fifth slot

    // Print the array elements
    for i := 0; i < len(arr); i++ {
        fmt.Print(arr[i])
        if i != len(arr)-1 {
            fmt.Print(" -> ")
        }
    }
    fmt.Println(" -> null")
}
var arr [5]int
Declare fixed-size array with 5 integer slots
arr[0] = 10 // assign 10 to first slot
Initialize first slot with 10
arr[1] = 20 // assign 20 to second slot
Initialize second slot with 20
arr[2] = 30 // assign 30 to third slot
Initialize third slot with 30
arr[3] = 40 // assign 40 to fourth slot
Initialize fourth slot with 40
arr[4] = 50 // assign 50 to fifth slot
Initialize fifth slot with 50
for i := 0; i < len(arr); i++ {
Traverse array from first to last slot
fmt.Print(arr[i])
Print current slot value
OutputSuccess
10 -> 20 -> 30 -> 40 -> 50 -> null
Complexity Analysis
Time: O(n) because we assign or access each of the n elements once
Space: O(n) because the array holds n elements in fixed memory
vs Alternative: Compared to dynamic structures like slices, arrays have fixed size and simpler memory layout but less flexibility
Edge Cases
Declare array without initialization
All slots contain zero values by default
DSA Go
var arr [5]int
Initialize array with fewer elements than size
Remaining slots get zero values automatically
DSA Go
var arr = [5]int{10, 20}
Access index out of bounds
Program panics at runtime (index error)
DSA Go
arr[5] = 60 // would cause panic
When to Use This Pattern
When you need a fixed-size collection of elements with fast access by position, use array declaration and initialization to set up the container.
Common Mistakes
Mistake: Trying to assign more elements than the declared array size
Fix: Ensure the number of elements does not exceed the declared array length
Mistake: Accessing array index outside valid range (0 to size-1)
Fix: Always check index is within bounds before accessing or assigning
Summary
Creates a fixed-size collection of elements and fills them with values.
Use when you know the exact number of elements and want fast indexed access.
An array is like a fixed egg carton where each slot holds one item in order.

Practice

(1/5)
1. Which of the following is the correct way to declare an array of 5 integers in Go?
easy
A. var arr []int = make([]int, 5)
B. var arr [5]int
C. arr := [5]int
D. arr := []int{5}

Solution

  1. Step 1: Understand array declaration syntax in Go

    Arrays in Go are declared with a fixed size inside square brackets, followed by the type, e.g., var arr [5]int.
  2. Step 2: Analyze each option

    var arr [5]int correctly declares an array of 5 integers. arr := [5]int uses shorthand but lacks the array literal {}, causing a syntax error. var arr []int = make([]int, 5) declares a slice, not an array. arr := []int{5} declares a slice with one element 5, not an array of size 5.
  3. Final Answer:

    var arr [5]int -> Option B
  4. Quick Check:

    Array declaration = var arr [5]int [OK]
Hint: Arrays use fixed size in brackets, slices do not [OK]
Common Mistakes:
  • Confusing slices with arrays
  • Using shorthand without initialization
  • Incorrect size placement
2. Which of the following is the correct syntax to initialize an array with values 1, 2, 3 in Go?
easy
A. arr := []int{1, 2, 3}
B. arr := [3]int(1, 2, 3)
C. var arr [3]int = {1, 2, 3}
D. var arr = [3]int{1, 2, 3}

Solution

  1. Step 1: Review array initialization syntax

    In Go, arrays are initialized with curly braces and values inside, e.g., [3]int{1, 2, 3}.
  2. Step 2: Check each option

    var arr = [3]int{1, 2, 3} uses correct syntax. arr := [3]int(1, 2, 3) uses parentheses which is invalid. var arr [3]int = {1, 2, 3} misses the type keyword before braces. arr := []int{1, 2, 3} declares a slice, not an array.
  3. Final Answer:

    var arr = [3]int{1, 2, 3} -> Option D
  4. Quick Check:

    Array init with braces = var arr = [3]int{1, 2, 3} [OK]
Hint: Use curly braces {} for array initialization [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Confusing slices with arrays
  • Omitting type in declaration
3. What will be the output of the following Go code?
package main
import "fmt"
func main() {
  var arr = [4]int{10, 20}
  fmt.Println(arr)
}
medium
A. [10 20 0 0]
B. [10 20]
C. [0 0 0 0]
D. Compilation error

Solution

  1. Step 1: Understand partial array initialization

    When an array is partially initialized, the remaining elements are set to zero values (0 for int).
  2. Step 2: Analyze the given array

    The array size is 4, but only first two elements are initialized as 10 and 20. The last two elements default to 0.
  3. Final Answer:

    [10 20 0 0] -> Option A
  4. Quick Check:

    Partial init fills rest with zeros = [10 20 0 0] [OK]
Hint: Uninitialized array elements default to zero [OK]
Common Mistakes:
  • Expecting only initialized elements in output
  • Thinking partial init causes error
  • Confusing slices with arrays
4. Identify the error in the following Go code snippet:
package main
func main() {
  var arr [3]int = [3]int{1, 2}
}
medium
A. No error, code is correct
B. Array size mismatch error
C. Syntax error in array declaration
D. Missing import statement

Solution

  1. Step 1: Check array size and initialization

    The array is declared with size 3 but initialized with 2 values. Go allows this and fills the last element with zero.
  2. Step 2: Verify syntax and imports

    No syntax error exists. Import is not mandatory if no external packages are used. The code compiles fine.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Partial init allowed, no import needed [OK]
Hint: Partial initialization is allowed; missing imports only if used [OK]
Common Mistakes:
  • Assuming size mismatch causes error
  • Expecting import for main package
  • Confusing slices with arrays
5. You want to declare an array of 5 integers in Go and initialize only the last element to 100, leaving others as zero. Which code snippet achieves this correctly?
hard
A. var arr = [5]int{0: 100}
B. var arr = [5]int{5: 100}
C. var arr = [5]int{4: 100}
D. var arr = [5]int{100}

Solution

  1. Step 1: Understand indexed initialization in arrays

    Go allows initializing specific elements by index using syntax like {index: value}. Indexing starts at 0.
  2. Step 2: Analyze each option

    var arr = [5]int{4: 100} initializes the element at index 4 (the last element in a 5-element array) to 100, others default to 0. var arr = [5]int{5: 100} uses index 5 which is out of range. var arr = [5]int{100} initializes only the first element. var arr = [5]int{0: 100} initializes index 0 to 100, not the last.
  3. Final Answer:

    var arr = [5]int{4: 100} -> Option C
  4. Quick Check:

    Use index 4 for last element in 5-size array [OK]
Hint: Index starts at 0; last element is index size-1 [OK]
Common Mistakes:
  • Using out-of-range index
  • Initializing first element instead of last
  • Omitting index for specific element
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard