100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Arrays in Go

A fixed-size, ordered collection of elements of the same type whose length is part of its type.

Data StructuresBeginner6 min readJul 8, 2026
Analogies

Introduction

An array in Go is a fixed-size sequence of elements that all share the same type. Unlike slices, the size of an array is part of its type definition, so [5]int and [10]int are different, incompatible types. Because arrays are rarely resized in real programs, Go developers usually reach for slices instead, but understanding arrays is the foundation for understanding slices, since a slice is a view over an underlying array.

🏏

Cricket analogy: A T20 squad list of exactly 15 named players is fixed for the tournament, just as [5]int locks its length into the type itself, so a 15-man squad and an 11-man playing XI are simply different, incompatible types.

Syntax

go
var numbers [5]int

primes := [5]int{1, 2, 3, 4, 5}

// Let the compiler count the elements
letters := [...]string{"a", "b", "c"}

// Indexed initialization
sparse := [5]int{0: 10, 4: 50}

Explanation

var numbers [5]int declares an array of 5 integers, each initialized to the zero value 0. primes := [5]int{1, 2, 3, 4, 5} uses a composite literal to set every element explicitly. The [...] syntax tells the compiler to infer the length from the number of elements provided, which is convenient when you don't want to count manually. You can also use indexed initialization like [5]int{0: 10, 4: 50} to set specific positions while leaving the rest at their zero value. Arrays are compared with == when their element type supports comparison, and assigning one array to another copies every element.

🏏

Cricket analogy: Declaring var numbers [5]int is like naming five untested net bowlers before trials, each starting at zero wickets, while primes := [5]int{1,2,3,4,5} is handing the selectors a finalized scorecard with every player's runs set explicitly.

Example

go
package main

import "fmt"

func main() {
	scores := [4]int{90, 85, 78, 92}

	sum := 0
	for i := 0; i < len(scores); i++ {
		sum += scores[i]
	}

	average := float64(sum) / float64(len(scores))
	fmt.Println("Scores:", scores)
	fmt.Println("Average:", average)

	// Arrays are copied on assignment
	copyOfScores := scores
	copyOfScores[0] = 100
	fmt.Println("Original untouched:", scores[0])
}

Output

go
Scores: [90 85 78 92]
Average: 86.25
Original untouched: 90

Key Takeaways

  • An array's length is part of its type, so [3]int and [5]int are different types.
  • Arrays are value types: assigning or passing one copies all its elements.
  • The zero value of an array has every element set to the zero value of its element type.
  • Use [...]T{...} to let the compiler infer the array length from the literal.
  • Slices, which are far more common in practice, are built on top of arrays.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#ArraysInGo#Arrays#Syntax#Explanation#Example#DataStructures#StudyNotes#SkillVeris