Introduction
In Go, an array is a numbered sequence of elements of a specific length. Arrays have a fixed size that’s part of their type. While slices are more common in typical Go code, arrays are useful in some special scenarios and form the foundation for understanding slices.
The Code
// In Go, an _array_ is a numbered sequence of elements of a
// specific length. In typical Go code, [slices](slices) are
// much more common; arrays are useful in some special
// scenarios.
package main
import "fmt"
func main() {
// Here we create an array `a` that will hold exactly
// 5 `int`s. The type of elements and length are both
// part of the array's type. By default an array is
// zero-valued, which for `int`s means `0`s.
var a [5]int
fmt.Println("emp:", a)
// We can set a value at an index using the
// `array[index] = value` syntax, and get a value with
// `array[index]`.
a[4] = 100
fmt.Println("set:", a)
fmt.Println("get:", a[4])
// The builtin `len` returns the length of an array.
fmt.Println("len:", len(a))
// Use this syntax to declare and initialize an array
// in one line.
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)
// You can also have the compiler count the number of
// elements for you with `...`
b = [...]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b)
// If you specify the index with `:`, the elements in
// between will be zeroed.
b = [...]int{100, 3: 400, 500}
fmt.Println("idx:", b)
// Array types are one-dimensional, but you can
// compose types to build multi-dimensional data
// structures.
var twoD [2][3]int
for i := range 2 {
for j := range 3 {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
// You can create and initialize multi-dimensional
// arrays at once too.
twoD = [2][3]int{
{1, 2, 3},
{1, 2, 3},
}
fmt.Println("2d: ", twoD)
}
Array Fundamentals
Array Declaration
var a [5]int
fmt.Println("emp:", a) // emp: [0 0 0 0 0]
This creates an array that holds exactly 5 integers. The length is part of the array’s type. Arrays are zero-valued by default (all elements set to 0 for integers).
The type [5]int is different from [6]int - the length is part of the type. You can’t assign one to the other.
Setting and Getting Values
a[4] = 100 // Set value at index 4
fmt.Println("get:", a[4]) // Get value at index 4
Use bracket notation for array access. Arrays are zero-indexed, so valid indices for [5]int are 0-4.
Array Length
fmt.Println("len:", len(a)) // len: 5
The len function returns the number of elements in the array.
Array Initialization
Explicit Initialization
b := [5]int{1, 2, 3, 4, 5}
Declare and initialize an array in one line with literal values.
Compiler-Counted Elements
b = [...]int{1, 2, 3, 4, 5}
Use ... to let the compiler count the elements. This is useful when you might change the number of elements.
Using ... makes it easier to add or remove elements from the initialization without manually updating the count.
Indexed Initialization
b = [...]int{100, 3: 400, 500}
// Result: [100 0 0 400 500]
You can specify indices explicitly. Unspecified elements between indices are zero-valued. Here:
- Index 0: 100
- Index 1-2: 0 (default)
- Index 3: 400
- Index 4: 500
Multi-Dimensional Arrays
Declaring 2D Arrays
This creates a 2x3 array (2 rows, 3 columns). The type is [2][3]int - an array of 2 elements, where each element is an array of 3 ints.
Populating 2D Arrays
for i := range 2 {
for j := range 3 {
twoD[i][j] = i + j
}
}
// Result: [[0 1 2] [1 2 3]]
Use nested loops to populate multi-dimensional arrays.
Initializing 2D Arrays
twoD = [2][3]int{
{1, 2, 3},
{1, 2, 3},
}
You can initialize multi-dimensional arrays with nested literals.
Arrays vs Slices
| Feature | Arrays | Slices |
|---|
| Length | Fixed, part of type | Dynamic |
| Passing to functions | Copies entire array | Passes reference |
| Common usage | Special cases | Most Go code |
| Type | [5]int | []int |
Important: Arrays are values. When you pass an array to a function, it gets a copy of the entire array. This is different from most other languages and is why slices are more common in Go.
When to Use Arrays
Use arrays when:
- You need a fixed-size collection
- The size is known at compile time
- You want value semantics (copying)
- You’re working with low-level code or specific algorithms
Most Go code uses slices instead because they’re more flexible.
Key Takeaways
- Arrays have a fixed length that’s part of their type
- Default values are the zero value for the element type
- Use
len() to get the array length
- Arrays can be multi-dimensional
- Use
... in initialization to let the compiler count elements
- Arrays are values and are copied when passed to functions
- In most cases, prefer slices over arrays for flexibility
- Access elements with zero-based indexing:
array[index]