What is a concise way to create a 2D slice in Go?

GoSlice

Go Problem Overview


I am learning Go by going through A Tour of Go. One of the exercises there asks me to create a 2D slice of dy rows and dx columns containing uint8. My current approach, which works, is this:

a:= make([][]uint8, dy)       // initialize a slice of dy slices
for i:=0;i<dy;i++ {
	a[i] = make([]uint8, dx)  // initialize a slice of dx unit8 in each of dy slices
}

I think that iterating through each slice to initialize it is too verbose. And if the slice had more dimensions, the code would become unwieldy. Is there a concise way to initialize 2D (or n-dimensional) slices in Go?

Go Solutions


Solution 1 - Go

There isn't a more concise way, what you did is the "right" way; because slices are always one-dimensional but may be composed to construct higher-dimensional objects. See this question for more details: https://stackoverflow.com/questions/39561140/go-how-is-two-dimensional-arrays-memory-representation.

One thing you can simplify on it is to use the for range construct:

a := make([][]uint8, dy)
for i := range a {
	a[i] = make([]uint8, dx)
}

Also note that if you initialize your slice with a composite literal, you get this for "free", for example:

a := [][]uint8{
	{0, 1, 2, 3},
	{4, 5, 6, 7},
}
fmt.Println(a) // Output is [[0 1 2 3] [4 5 6 7]]

Yes, this has its limits as seemingly you have to enumerate all the elements; but there are some tricks, namely you don't have to enumerate all values, only the ones that are not the zero values of the element type of the slice. For more details about this, see https://stackoverflow.com/questions/36302441/keyed-items-in-golang-array-initialization.

For example if you want a slice where the first 10 elements are zeros, and then follows 1 and 2, it can be created like this:

b := []uint{10: 1, 2}
fmt.Println(b) // Prints [0 0 0 0 0 0 0 0 0 0 1 2]

Also note that if you'd use arrays instead of slices, it can be created very easily:

c := [5][5]uint8{}
fmt.Println(c)

Output is:

[[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]

In case of arrays you don't have to iterate over the "outer" array and initialize "inner" arrays, as arrays are not descriptors but values. See blog post Arrays, slices (and strings): The mechanics of 'append' for more details.

Try the examples on the Go Playground.

Solution 2 - Go

There are two ways to use slices to create a matrix. Let's take a look at the differences between them.

First method:

matrix := make([][]int, n)
for i := 0; i < n; i++ {
	matrix[i] = make([]int, m)
}

Second method:

matrix := make([][]int, n)
rows := make([]int, n*m)
for i := 0; i < n; i++ {
	matrix[i] = rows[i*m : (i+1)*m]
}

In regards to the first method, making successive make calls doesn't ensure that you will end up with a contiguous matrix, so you may have the matrix divided in memory. Let's think of an example with two Go routines that could cause this:

  1. The routine #0 runs make([][]int, n) to get allocated memory for matrix, getting a piece of memory from 0x000 to 0x07F.
  2. Then, it starts the loop and does the first row make([]int, m), getting from 0x080 to 0x0FF.
  3. In the second iteration it gets preempted by the scheduler.
  4. The scheduler gives the processor to routine #1 and it starts running. This one also uses make (for its own purposes) and gets from 0x100 to 0x17F (right next to the first row of routine #0).
  5. After a while, it gets preempted and routine #0 starts running again.
  6. It does the make([]int, m) corresponding to the second loop iteration and gets from 0x180 to 0x1FF for the second row. At this point, we already got two divided rows.

With the second method, the routine does make([]int, n*m) to get all the matrix allocated in a single slice, ensuring contiguity. After that, a loop is needed to update the matrix pointers to the subslices corresponding to each row.

You can play with the code shown above in the Go Playground to see the difference in the memory assigned by using both methods. Note that I used runtime.Gosched() only with the purpose of yielding the processor and forcing the scheduler to switch to another routine.

Which one to use? Imagine the worst case with the first method, i.e. each row is not next in memory to another row. Then, if your program iterates through the matrix elements (to read or write them), there will probably be more cache misses (hence higher latency) compared to the second method because of worse data locality. On the other hand, with the second method it may not be possible to get a single piece of memory allocated for the matrix, because of memory fragmentation (chunks spread all over the memory), even though theoretically there may be enough free memory for it.

Therefore, unless there's a lot of memory fragmentation and the matrix to be allocated is huge enough, you would always want to use the second method to get advantage of data locality.

Solution 3 - Go

With Go 1.18 you get generics.

Here is a function that uses generics to allow to create a 2D slice for any cell type.

func Make2D[T any](n, m int) [][]T {
	matrix := make([][]T, n)
	rows := make([]T, n*m)
	for i, startRow := 0, 0; i < n; i, startRow = i+1, startRow+m {
		endRow := startRow + m
		matrix[i] = rows[startRow:endRow:endRow]
	}
	return matrix
}

With that function in your toolbox, your code becomes:

a := Make2D[uint8](dy, dx)

You can play with the code on the Go Playground.

Solution 4 - Go

Here a consive way to do it:

value := [][]string{}{[]string{}{"A1","A2"}, []string{}{"B1", "B2"}}

PS.: you can change "string" to the type of element you're using in your slice.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhazrmardView Question on Stackoverflow
Solution 1 - GoiczaView Answer on Stackoverflow
Solution 2 - GoMarcos Canales MayoView Answer on Stackoverflow
Solution 3 - GodolmenView Answer on Stackoverflow
Solution 4 - GoLucas Eduardo CoelhoView Answer on Stackoverflow