Arrays are fixed-size collections in Go. Understanding arrays is essential before working with slices, which are built on top of arrays. For more context, see Go Installation Guide, Go Ecosystem Overview, Go Best Practices.
Array Basics
Creating Arrays
package main
import "fmt"
func main() {
// Array with explicit size
var arr [5]int
fmt.Println(arr) // [0 0 0 0 0]
// Array with initialization
arr2 := [5]int{1, 2, 3, 4, 5}
fmt.Println(arr2)
// Array with partial initialization
arr3 := [5]int{1, 2, 3}
fmt.Println(arr3) // [1 2 3 0 0]
// Array with inferred size
arr4 := [...]int{1, 2, 3, 4, 5}
fmt.Println(len(arr4)) // 5
}
Array Types
package main
import "fmt"
func main() {
// Different array types
intArr := [3]int{1, 2, 3}
strArr := [3]string{"a", "b", "c"}
floatArr := [3]float64{1.1, 2.2, 3.3}
boolArr := [3]bool{true, false, true}
fmt.Println(intArr, strArr, floatArr, boolArr)
}
Indexing
Accessing Elements
package main
import "fmt"
func main() {
arr := [5]int{10, 20, 30, 40, 50}
// Access by index
fmt.Println(arr[0]) // 10
fmt.Println(arr[2]) // 30
fmt.Println(arr[4]) // 50
// Modify element
arr[2] = 35
fmt.Println(arr) // [10 20 35 40 50]
}
Array Length
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
fmt.Println(len(arr)) // 5
// Iterate using length
for i := 0; i < len(arr); i++ {
fmt.Println(arr[i])
}
}
Out of Bounds
package main
import "fmt"
func main() {
arr := [3]int{1, 2, 3}
// This will panic at runtime
// fmt.Println(arr[5]) // panic: index out of range
}
Slicing
Creating Slices from Arrays
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
// Slice from index 1 to 4 (exclusive)
slice1 := arr[1:4]
fmt.Println(slice1) // [2 3 4]
// Slice from beginning
slice2 := arr[:3]
fmt.Println(slice2) // [1 2 3]
// Slice to end
slice3 := arr[2:]
fmt.Println(slice3) // [3 4 5]
// Full slice
slice4 := arr[:]
fmt.Println(slice4) // [1 2 3 4 5]
}
Slice Behavior
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1:4]
fmt.Println(slice) // [2 3 4]
// Modify through slice
slice[0] = 20
fmt.Println(arr) // [1 20 3 4 5]
fmt.Println(slice) // [20 3 4]
}
Iterating Arrays
Using For Loop
package main
import "fmt"
func main() {
arr := [5]int{10, 20, 30, 40, 50}
// Traditional for loop
for i := 0; i < len(arr); i++ {
fmt.Println(i, arr[i])
}
}
Using Range
package main
import "fmt"
func main() {
arr := [5]int{10, 20, 30, 40, 50}
// Range with index and value
for i, v := range arr {
fmt.Printf("Index: %d, Value: %d\n", i, v)
}
// Range with index only
for i := range arr {
fmt.Println(i)
}
// Range with value only
for _, v := range arr {
fmt.Println(v)
}
}
Multi-dimensional Arrays
2D Arrays
package main
import "fmt"
func main() {
// 2D array
matrix := [3][3]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
fmt.Println(matrix)
fmt.Println(matrix[0][0]) // 1
fmt.Println(matrix[1][2]) // 6
// Iterate 2D array
for i := 0; i < len(matrix); i++ {
for j := 0; j < len(matrix[i]); j++ {
fmt.Printf("%d ", matrix[i][j])
}
fmt.Println()
}
}
Arrays vs Slices
Key Differences
package main
import "fmt"
func main() {
// Array - fixed size
arr := [5]int{1, 2, 3, 4, 5}
fmt.Printf("Array type: %T, Length: %d\n", arr, len(arr))
// Slice - dynamic size
slice := []int{1, 2, 3, 4, 5}
fmt.Printf("Slice type: %T, Length: %d, Capacity: %d\n", slice, len(slice), cap(slice))
// Slice from array
slice2 := arr[:]
fmt.Printf("Slice from array: %T\n", slice2)
}
Best Practices
✅ Good: Use Arrays for Fixed Size
// DO: Use arrays when size is known and fixed
func processCoordinates(coords [3]float64) {
// Process 3D coordinates
}
❌ Bad: Use Arrays for Dynamic Size
// DON'T: Use arrays when size varies
var data [1000]int // Wasteful if you only use 10 elements
✅ Good: Use Slices for Dynamic Collections
// DO: Use slices for dynamic collections
func processItems(items []string) {
// Process variable number of items
}
✅ Good: Check Bounds
// DO: Check array bounds
if index >= 0 && index < len(arr) {
value := arr[index]
}
Summary
Go arrays provide:
- Fixed-size collections with compile-time size
- Efficient memory layout
- Type safety with specific element types
- Slicing for creating views
- Foundation for slices
Understanding arrays is crucial for mastering Go’s data structures.
Comments