Go Arrays

Su Suriya Ravichandran Updated 14 Sep 2026
3 min read ·Lesson 9 of 12

An array stores a fixed number of elements of the same type in a single variable. Instead of creating separate variables for each value, an array groups related data together and lets you access any element by its position.

What Is an Array?

An array:

  • Holds a fixed number of elements, set when it is declared.
  • Stores elements that are all the same type.
  • Uses zero-based indexing — the first element is at index 0.

Important: an array's length is part of its type in Go. A [4]int and a [5]int are different types entirely, and this fixed size is exactly why slices (covered in the next lesson) are used far more often than arrays in everyday Go code.

Declaring Arrays

// Using var
var scores = [4]int{85, 90, 78, 92}

// Using short declaration
marks := [3]int{70, 80, 95}

Letting Go Infer the Length

The ... syntax tells the compiler to count the elements in the literal and set the array size automatically:

package main

import "fmt"

func main() {
    primes := [...]int{2, 3, 5, 7, 11}

    fmt.Println("Prime numbers:", primes)
}

Declaring and Printing Arrays

package main

import "fmt"

func main() {
    temperatures := [3]float64{36.5, 37.0, 36.8}
    cities := [...]string{"Chennai", "Delhi", "Mumbai"}

    fmt.Println("Temperatures:", temperatures)
    fmt.Println("Cities:", cities)
}

Accessing Array Elements

package main

import "fmt"

func main() {
    prices := [4]int{100, 200, 300, 400}

    fmt.Println("First price:", prices[0])
    fmt.Println("Last price:", prices[3])
}

Common mistake: since indexing starts at 0, the last valid index of an array is always length - 1. Accessing prices[4] on a 4-element array compiles (if the index is a variable, not a literal) but panics at runtime with index out of range — Go does not silently return a zero value or nil the way some languages do.

Modifying Array Elements

package main

import "fmt"

func main() {
    stock := [3]int{10, 20, 30}

    stock[1] = 50 // update the second element

    fmt.Println("Updated stock:", stock)
}

Default Values

If an array is declared without explicit values for every element, Go fills the rest with that type's zero value:

  • int0
  • string""
  • boolfalse
package main

import "fmt"

func main() {
    numbers := [5]int{}          // all five values default to 0
    partial := [5]int{1, 2, 3}   // first three set; remaining two default to 0

    fmt.Println("Numbers:", numbers)
    fmt.Println("Partial:", partial)
}

Initializing Specific Elements by Index

You can target specific indices directly in the literal, leaving everything else at its zero value:

package main

import "fmt"

func main() {
    data := [5]int{0: 10, 3: 40}

    fmt.Println("Custom array:", data)
}

Expected output:

Custom array: [10 0 0 40 0]

Index 0 is set to 10, index 3 is set to 40, and every other index defaults to 0.

Finding an Array's Length

The built-in len() function returns the number of elements:

package main

import "fmt"

func main() {
    languages := [...]string{"Go", "Python", "JavaScript"}

    fmt.Println("Number of languages:", len(languages))
}

Arrays Are Copied by Value

A detail that trips up many newcomers coming from languages like Python or JavaScript: assigning an array to a new variable, or passing it to a function, copies the entire array. The two arrays are then completely independent.

package main

import "fmt"

func main() {
    original := [3]int{1, 2, 3}
    copyArr := original

    copyArr[0] = 100

    fmt.Println("Original:", original) // unaffected
    fmt.Println("Copy:", copyArr)
}

Expected output:

Original: [1 2 3]
Copy: [100 2 3]

This copy-by-value behavior is one of the main practical reasons Go programs favor slices over arrays: a slice can be passed around cheaply because it only copies a small reference structure, not the underlying data. The next lesson covers this in detail.

0 Comments

Reviewed before they appear

No comments yet.

Go Lang
Ask about this post
AI Ask about this post

Ask questions about Go Arrays and get answers drawn from it.

Signed-in readers only.