Go Arrays

An array stores a fixed number of elements of the same type in a sequence. The size of an array is set when it is declared and cannot change afterward. Arrays in Go are value types — when an array is assigned to another variable, a full copy is made.

Declaring an Array

package main

import "fmt"

func main() {
    var scores [5]int
    fmt.Println(scores) // [0 0 0 0 0]
}

Go fills every position with the zero value of the element type — 0 for integers, "" for strings, false for booleans.

Array with Initial Values

package main

import "fmt"

func main() {
    fruits := [3]string{"Apple", "Banana", "Mango"}
    fmt.Println(fruits)    // [Apple Banana Mango]
    fmt.Println(fruits[0]) // Apple
    fmt.Println(fruits[2]) // Mango
}

Array Memory Diagram

fruits := [3]string{"Apple", "Banana", "Mango"}

Index:    0         1         2
       ┌────────┬──────────┬───────┐
       │ Apple  │  Banana  │ Mango │
       └────────┴──────────┴───────┘

fruits[0] → "Apple"
fruits[1] → "Banana"
fruits[2] → "Mango"

Let Go Count the Size – [...]

Use ... as the size and Go counts the elements automatically.

package main

import "fmt"

func main() {
    days := [...]string{"Mon", "Tue", "Wed", "Thu", "Fri"}
    fmt.Println(len(days)) // 5
}

Updating Array Elements

package main

import "fmt"

func main() {
    numbers := [4]int{10, 20, 30, 40}
    numbers[2] = 99
    fmt.Println(numbers) // [10 20 99 40]
}

Iterating Over an Array

package main

import "fmt"

func main() {
    marks := [5]int{70, 85, 90, 60, 78}

    total := 0
    for _, mark := range marks {
        total += mark
    }

    fmt.Println("Total:", total)
    fmt.Printf("Average: %.1f\n", float64(total)/float64(len(marks)))
}

Output:

Total: 383
Average: 76.6

Two-Dimensional Array

A 2D array is an array of arrays — useful for grids and tables.

package main

import "fmt"

func main() {
    grid := [2][3]int{
        {1, 2, 3},
        {4, 5, 6},
    }

    for _, row := range grid {
        for _, val := range row {
            fmt.Printf("%d ", val)
        }
        fmt.Println()
    }
}

Output:

1 2 3
4 5 6

Arrays Are Value Types

Assigning one array to another copies all the data. Changes to the copy do not affect the original.

package main

import "fmt"

func main() {
    a := [3]int{1, 2, 3}
    b := a         // full copy of a
    b[0] = 100

    fmt.Println(a) // [1 2 3] — unchanged
    fmt.Println(b) // [100 2 3]
}

Arrays vs Slices

FeatureArraySlice
SizeFixed at declarationDynamic — grows as needed
TypeValue type (copied)Reference type (shared)
Use caseFixed-size data like days of weekMost real-world list use cases

Key Points

  • Arrays have a fixed size that cannot change after declaration
  • Index starts at 0; the last valid index is length minus 1
  • Use [...] to let Go count elements automatically
  • Arrays are value types — assignment copies the entire array
  • In most Go programs, slices are preferred over arrays for their flexibility

Leave a Comment