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
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
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
Scores: [90 85 78 92]
Average: 86.25
Original untouched: 90Key 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
1. What is true about the length of an array in Go?
2. What happens when you assign one array to another variable of the same type?
3. What does `letters := [...]string{"a", "b", "c"}` produce?
4. Which statement about array element access is correct?
5. Why do idiomatic Go programs usually prefer slices over raw arrays?
Was this page helpful?
You May Also Like
Slices in Go
A dynamically-sized, flexible view into an underlying array, the go-to collection type in idiomatic Go.
Variables and Data Types in Go
Learn how Go declares variables with var and :=, and the built-in types like int, float64, bool, and string.
for Loops in Go
Master Go's single looping construct, which covers classic for, while-style, infinite, and range-based loops.
Pointers in Go
A variable that holds the memory address of another value, enabling shared access and mutation without copying.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics