π§± Go β Arrays Explained with Syntax, Examples, and Use Cases (2025 Guide)
π§² Introduction β What Are Arrays in Go?
In Go, an array is a fixed-size collection of elements of the same type. Unlike slices, arrays are not resizable, making them ideal for static data structures, fixed buffers, and low-level memory-safe operations. Each array has a known size and indexed elements starting from 0.
π― In this section, youβll learn:
- How to declare, initialize, and access arrays
- Array indexing and iteration techniques
- Difference between arrays and slices
- Common use cases and limitations
β Basic Syntax β Declare and Initialize Arrays
var a [3]int
a[0] = 10
a[1] = 20
a[2] = 30
fmt.Println(a)
π€ Output:
[10 20 30]
β
a is an array of 3 integers with values assigned using indices.
π Short Declaration and Initialization
b := [3]string{"Go", "Java", "Rust"}
fmt.Println(b)
π€ Output:
[Go Java Rust]
β You can also let Go infer the size:
c := [...]float64{3.14, 1.61, 2.71}
fmt.Println(len(c)) // Output: 3
π’ Accessing Elements by Index
names := [3]string{"Alice", "Bob", "Charlie"}
fmt.Println(names[1]) // Output: Bob
β
Index starts at 0 and ends at length - 1.
π Iterating Over Arrays β for Loop
arr := [4]int{10, 20, 30, 40}
for i := 0; i < len(arr); i++ {
fmt.Println("Index", i, "=", arr[i])
}
β
Classic for loop with index access.
π Iterating with range
arr := [3]int{5, 10, 15}
for i, v := range arr {
fmt.Println("Index:", i, "Value:", v)
}
β
range provides both index and value.
βοΈ Arrays vs Slices in Go
| Feature | Arrays | Slices |
|---|---|---|
| Size | Fixed at compile time | Dynamic (resizable) |
| Memory | Value type (copied on assignment) | Reference type (backed by array) |
| Use Case | Buffers, static-size collections | Dynamic lists, most Go programs |
| Syntax | [N]Type{} | []Type{} |
β οΈ Arrays Are Value Types
When you assign an array to another variable, Go makes a copy, not a reference:
a := [3]int{1, 2, 3}
b := a
b[0] = 100
fmt.Println(a) // [1 2 3]
fmt.Println(b) // [100 2 3]
β Use slices if you want reference-like behavior.
π§ Best Practices
- β Use arrays for fixed-size collections (e.g., days of the week, RGB values)
- β Donβt use arrays for dynamic listsβprefer slices
- β
Use
[...]Type{}for inferred-size declarations - β
Iterate with
rangefor readability and performance
π Summary β Recap & Next Steps
Arrays in Go are efficient and predictable, ideal for situations where size doesnβt change. While theyβre less flexible than slices, arrays give you better control and memory safety.
π Key Takeaways:
- Arrays are fixed-length, same-type data containers
- Use index-based or
range-based access - Arrays are value typesβassignments are copied
- Prefer slices for flexible, real-world data lists
βοΈ Next: Learn about Go Slices, which are dynamic views into arrays and more commonly used in modern Go code.
β FAQs β Go Arrays
β Can I change the size of an array in Go?
β No. Arrays are fixed-size. Use slices for dynamic resizing.
β Are arrays in Go value or reference types?
β
Arrays are value types. Assigning one array to another copies the data.
β How do I get the length of an array in Go?
β
Use len(array).
β What is the zero value of an array?
β
All elements are initialized to the zero value of their type (e.g., 0 for int, "" for string).
β Can I loop through arrays with range?
β
Yes. range gives index and value on each iteration.
Share Now :
