Lesson 1
Mastering Array Traversal and Manipulation in Go
Topic Overview

Welcome to today's session on "Mastering Array Traversal and Manipulation in Go". Multidimensional arrays in Go are like an 'apartment building' with floors (the outer array) and apartments on each floor (the inner array). Today, our goal is to enhance your understanding of these 'apartment buildings' and how to effectively manage them using Go's syntax and data structures.

Creating Multidimensional Arrays

In Go, we create a multidimensional array by defining arrays containing arrays. Let's see how to create and work with 2D arrays.

Go
1package main 2 3import "fmt" 4 5func main() { 6 // Creating a 2D array 7 array := [3][3]int{ 8 {1, 2, 3}, 9 {4, 5, 6}, 10 {7, 8, 9}, 11 } 12 13 // Printing the 2D array 14 for i := 0; i < len(array); i++ { 15 for j := 0; j < len(array[i]); j++ { 16 fmt.Print(array[i][j], " ") 17 } 18 fmt.Println() 19 } 20} 21 22/* 23Prints: 241 2 3 254 5 6 267 8 9 27*/
Indexing in Multidimensional Arrays

Indices in Go arrays, like in many programming languages, are 0-based. To access a specific element, we specify the indices. For example, visiting the apartment on the second floor (index 1) and delivering a package to the first unit (index 0):

Go
1package main 2 3import "fmt" 4 5func main() { 6 array := [3][3]int{ 7 {1, 2, 3}, 8 {4, 5, 6}, 9 {7, 8, 9}, 10 } 11 12 // Accessing an element 13 fmt.Println(array[1][0]) // Outputs: 4 14}

We've accessed the element 4 in the array by its row and column indices.

Finding the Number of Rows and Columns

In Go, to determine the number of rows (floors) and columns (apartments per floor), we use the len function, which returns the length of an array dimension.

Go
1package main 2 3import "fmt" 4 5func main() { 6 array := [3][3]int{ 7 {1, 2, 3}, 8 {4, 5, 6}, 9 {7, 8, 9}, 10 } 11 12 // Finding the number of rows 13 numFloors := len(array) 14 fmt.Println(numFloors) // Outputs: 3 15 16 // Finding the number of columns 17 numUnits := len(array[0]) 18 fmt.Println(numUnits) // Outputs: 3 19}
Traversing Multidimensional Arrays

To visit each floor (outer array) and each apartment on each floor (inner array), we use nested loops.

Go
1package main 2 3import "fmt" 4 5func main() { 6 array := [3][3]string{ 7 {"Apt 101", "Apt 102", "Apt 103"}, 8 {"Apt 201", "Exit Floor", "Apt 203"}, 9 {"Apt 301", "Apt 302", "Apt 303"}, 10 } 11 12 // Loop through the 2D array 13 for i := 0; i < len(array); i++ { 14 for j := 0; j < len(array[i]); j++ { 15 fmt.Print(array[i][j], ", ") 16 } 17 fmt.Println() 18 } 19} 20 21/* 22Prints: 23Apt 101, Apt 102, Apt 103, 24Apt 201, Exit Floor, Apt 203, 25Apt 301, Apt 302, Apt 303, 26*/
Updating Multidimensional Arrays

Continuing with the apartment building analogy, to replace an element (such as a locker code), modify it directly using its indices.

Go
1package main 2 3import "fmt" 4 5func main() { 6 array := [3][3]int{ 7 {1, 2, 3}, 8 {4, 5, 6}, 9 {7, 8, 9}, 10 } 11 12 // Updating an element 13 array[0][1] = 10 14 for i := 0; i < len(array); i++ { 15 for j := 0; j < len(array[i]); j++ { 16 fmt.Print(array[i][j], " ") 17 } 18 fmt.Println() 19 } 20} 21 22/* 23Prints: 241 10 3 254 5 6 267 8 9 27*/
Adding a New Row or Column

In Go, to add a new row or column to an array, we often use slices since arrays are of fixed size. Here's how you can simulate adding a row:

Go
1package main 2 3import "fmt" 4 5func main() { 6 array := [][]int{ 7 {1, 2, 3}, 8 {4, 5, 6}, 9 {7, 8, 9}, 10 } 11 12 // Add a new row 13 newArray := append(array, []int{10, 11, 12}) 14 15 // Display the new array 16 for _, row := range newArray { 17 for _, element := range row { 18 fmt.Print(element, " ") 19 } 20 fmt.Println() 21 } 22} 23 24/* 25Prints: 261 2 3 274 5 6 287 8 9 2910 11 12 30*/
Removing a Row or Column

To remove a row or column in Go, you can manipulate slices by re-slicing or appending slices that exclude the unwanted element.

Go
1package main 2 3import "fmt" 4 5func main() { 6 array := [][]int{ 7 {1, 2, 3}, 8 {4, 5, 6}, 9 {7, 8, 9}, 10 } 11 12 // Remove the second row (index 1) 13 rowRemovedArray := append(array[:1], array[2:]...) 14 15 // Display the new array (with row removed) 16 for _, row := range rowRemovedArray { 17 for _, element := range row { 18 fmt.Print(element, " ") 19 } 20 fmt.Println() 21 } 22} 23 24/* 25Prints: 261 2 3 277 8 9 28*/
Lesson Summary and Practice

Today, we covered various operations on multidimensional arrays in Go, starting from their creation to methods of updating them using Go's syntax. We also learned how to visit each floor and apartment using loops. Enrich your learning experience by experimenting with these concepts in practical exercises focusing on multidimensional arrays in Go. Enjoy your coding journey!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.