Skip to content

Arrays

An array is a fixed‑size sequence of elements of a single type. The size is part of the type (e.g., [5]int). Arrays are value types – assigning copies the entire array.

Declare, initialize, and iterate over arrays.

package main
import "fmt"
func main() {
var a [3]int // zero-valued array [0,0,0]
b := [4]int{1, 2, 3, 4} // literal
c := [...]int{10, 20, 30} // size inferred
a[0] = 42
fmt.Println(a, b, c)
// Iterate
for i, v := range c {
fmt.Printf("c[%d] = %d\n", i, v)
}
}
Terminal window
[42 0 0] [1 2 3 4] [10 20 30]
c[0] = 10
c[1] = 20
c[2] = 30