HomeSearch

Golang 2D Slices and Arrays

Use 2D slices and arrays. Nest slices and arrays to create multiple dimensions.

2D slice, array.

In this digital world we check the map. Our destination is the lake. This map has just 2 dimensions—but that is enough for navigation.

In the Go language

we model 2D things (like the adventurer's map) with slices of slices. In the two levels of nesting we store the two dimensions.

Slice example.

This program creates an empty slice of slices. It then creates 2 rows, which are also slices. With append() we add the two rows to the "values."

Jagged: We could use the term "jagged" to describe nested slices. Each sub-slice can be any number of elements long.

Access: We can access an entire sub-slice with an index number. With 2 indexes, we get or set a single element.

Golang program that uses 2d slices package main import "fmt" func main() { values := [][]int{} // These are the first two rows. row1 := []int{1, 2, 3} row2 := []int{4, 5, 6} // Append each row to the two-dimensional slice. values = append(values, row1) values = append(values, row2) // Display first row. fmt.Println("Row 1") fmt.Println(values[0]) // Display second row. fmt.Println("Row 2") fmt.Println(values[1]) // Access an element. fmt.Println("First element") fmt.Println(values[0][0]) // Display entire slice. fmt.Println("Values") fmt.Println(values) } Output Row 1 [1 2 3] Row 2 [4 5 6] First element 1 Values [[1 2 3] [4 5 6]]

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.

Golang program that creates 2D array 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) } Output [[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.

Range: The range built-in returns all indexes (in order) of the animals slice. We then access each nested slice of animal names.

Range
Golang program that uses nested string slices 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]) } } Output 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.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.