Arrays: Fixed-Length Collections
Array literals, indexing, and length
What Is an Array?
An array in Go is a fixed-length sequence of elements of the same type. The length is part of the type: [3]int and [4]int are different types.
- Arrays are value types — assigning copies the entire array
- Length is always known at compile time
- Rarely used directly; slices are preferred for most tasks
Array Literals
Declare and initialize arrays with a literal:
package main
func main() {
primes := [5]int{2, 3, 5, 7, 11}
names := [3]string{"Alice", "Bob", "Carol"}
flags := [4]bool{true, false, true, false}
_ = primes; _ = names; _ = flags
}All lessons in this course
- Arrays: Fixed-Length Collections
- Slices: Dynamic Lists
- Maps: Key-Value Stores
- Iterating Collections with range