🧮 Go Multidimensional Arrays – Work with 2D and 3D Array Structures (2025 Guide)
🧲 Introduction – What Are Multidimensional Arrays in Go?
Multidimensional arrays in Go are arrays of arrays—ideal for representing grids, tables, matrices, and more. They allow you to store and access elements using multiple indices, such as array[row][column] in 2D arrays.
🎯 In this section, you’ll learn:
- How to declare and initialize 2D arrays in Go
- How to access and modify elements with multiple indices
- Iterate over rows and columns using nested loops
- Use cases and differences from slices of slices
✅ Basic Syntax – Declare a 2D Array
var matrix [2][3]int
✅ This creates a 2×3 matrix (2 rows, 3 columns) initialized with zeros:
fmt.Println(matrix)
// Output: [[0 0 0] [0 0 0]]
🧩 Initializing a Multidimensional Array
grid := [2][3]int{
{1, 2, 3},
{4, 5, 6},
}
fmt.Println(grid)
📤 Output:
[[1 2 3] [4 5 6]]
✅ Each inner array represents a row in the 2D structure.
🔢 Accessing and Modifying Elements
grid[1][2] = 9
fmt.Println(grid[1][2]) // Output: 9
✅ Use array[row][column] format to access or change values.
🔁 Iterating Over a 2D Array
matrix := [2][3]int{
{10, 20, 30},
{40, 50, 60},
}
for i := 0; i < len(matrix); i++ {
for j := 0; j < len(matrix[i]); j++ {
fmt.Printf("matrix[%d][%d] = %d\n", i, j, matrix[i][j])
}
}
📤 Output:
matrix[0][0] = 10
matrix[0][1] = 20
matrix[0][2] = 30
matrix[1][0] = 40
matrix[1][1] = 50
matrix[1][2] = 60
✅ Use nested loops to traverse each element.
🧠 Difference: Multidimensional Arrays vs Slices of Slices
| Feature | Multidimensional Array | Slice of Slices |
|---|---|---|
| Memory Layout | Contiguous block | Separate underlying arrays |
| Size | Fixed | Dynamic |
| Declaration | [3][2]int | [][]int |
| Flexibility | ❌ Limited | ✅ Highly flexible |
✅ Use slices of slices if the number of rows/columns is dynamic.
🧠 3D Arrays Example
var cube [2][2][2]int = [2][2][2]int{
{
{1, 2},
{3, 4},
},
{
{5, 6},
{7, 8},
},
}
fmt.Println(cube[1][0][1]) // Output: 6
✅ A 3D array has 3 indices: depth, row, column.
📌 Summary – Recap & Next Steps
Multidimensional arrays in Go help manage structured data in a grid-like format. While less flexible than slices, they’re memory-efficient and safe when sizes are known at compile time.
🔍 Key Takeaways:
- Use
[rows][cols]Typeto define 2D arrays - Access elements using chained indices:
array[i][j] - Use nested
forloops for traversal - Prefer slices of slices for variable-sized data structures
⚙️ Next: Explore Slices of Slices in Go to create flexible, dynamic multi-dimensional data.
❓ FAQs – Go Multidimensional Arrays
❓ How do I declare a 2D array in Go?
✅ Use syntax like var a [3][4]int for 3 rows and 4 columns.
❓ Can I change the size of a multidimensional array at runtime?
❌ No. Arrays in Go are fixed-size. Use slices for dynamic sizes.
❓ Are Go multidimensional arrays stored contiguously?
✅ Yes. Unlike slices of slices, they are stored in a single memory block.
❓ How do I initialize a multidimensional array?
✅ Nest inner arrays inside outer ones:
arr := [2][2]int{{1,2}, {3,4}}
❓ What’s the difference between [2][3]int and [][]int?
✅ [2][3]int is a fixed-size 2D array; [][]int is a slice of slices and more flexible.
Share Now :
