Home
Go
2D Slice, Array Examples
This page was last reviewed on Apr 21, 2023.
Dot Net Perls
2D slice, array. In the Go language we model 2D things with slices of slices. In the 2 levels of nesting we store the 2 dimensions.
Shows a 2d slice
Appending data. To create a 2D slice, we use append() to add each row. Then we can access the data by indexing through the slices.
Slice example. Consider a slice of slices: we could use the term "jagged" to describe nested slices. Each sub-slice can be any number of elements long.
Slice
Step 1 Initially we create an empty slice of slices—it has no top-level slice elements.
Step 2 Here we create 2 rows (which are also slices). And with append() we add the two rows to the "values."
Step 3 We can access entire rows with the first index. Row 0 is the first row, and row 1 is the second row.
Step 4 With 2 indexes, we address a single int element. We can get or set an integer in the nested slices.
Shows a 2d slice
package main import "fmt" func main() { // Step 1: create empty collection. values := [][]int{} // Step 2: these are the first two rows. // ... Append each row to the two-dimensional slice. row1 := []int{1, 2, 3} row2 := []int{4, 5, 6} values = append(values, row1) values = append(values, row2) // Step 3: display first row, and second row. fmt.Println("Row 1") fmt.Println(values[0]) fmt.Println("Row 2") fmt.Println(values[1]) // Step 4: access an element. fmt.Println("First element") fmt.Println(values[0][0]) }
Row 1 [1 2 3] Row 2 [4 5 6] First element 1
2D arrays. To create a 2D array we must specify each dimension. We can then assign individual elements within the array. Here we create a 2 by 2 array of strings.
Tip Arrays in Go have fixed sizes. We must specify the size of the array. For variable-size collections, slices are better.
package main import "fmt" func main() { // Create two-dimensional array. letters := [2][2]string{} // Assign all elements in 2 by 2 array. letters[0][0] = "a" letters[0][1] = "b" letters[1][0] = "c" letters[1][1] = "d" // Display result. fmt.Println(letters) }
[[a b] [c d]]
Nested string slices. Here we use strings in nested slices. Each slice does not have the same length: this is a jagged slice. We loop over the nested slices with for.
Detail The range built-in returns all indexes (in order) of the animals slice. We then access each nested slice of animal names.
for
package main import "fmt" func main() { // Create an empty slice of slices. animals := [][]string{} // Create three string slices. row1 := []string{"fish", "shark", "eel"} row2 := []string{"bird"} row3 := []string{"lizard", "salamander"} // Append string slices to outer slice. animals = append(animals, row1) animals = append(animals, row2) animals = append(animals, row3) // Loop over slices in animals. for i := range animals { fmt.Printf("Row: %v\n", i) fmt.Println(animals[i]) } }
Row: 0 [fish shark eel] Row: 1 [bird] Row: 2 [lizard salamander]
Some data sources are often stored in a two-dimensional plane. This can be efficient. But often, using a map is a good option—it saves space in sparse collections.
A review. Idiomatic Go is code that matches the language's popular usage. In Go we usually prefer slices (not arrays). So a 2D slice is an ideal solution.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 21, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.