6

I'm getting a surprising result when selecting a 2D sub-slice of a slice. Consider the following 2D int array

a := [][]int{
    {0, 1, 2, 3},
    {1, 2, 3, 4},
    {2, 3, 4, 5},
    {3, 4, 5, 6},
}

To select the top left 3x3 2D slice using ranges I would use

b := a[0:2][0:2]

I would expect the result to be

[[0 1 2] [1 2 3] [2 3 4]]

however the second index range doesn't seem to have any effect, and returns the following instead:

[[0 1 2 3] [1 2 3 4] [2 3 4 5]]

What am I missing? Can you simply not select a sub-slice like this where the dimension > 1 ?

icza
  • 389,944
  • 63
  • 907
  • 827
Jonas D
  • 258
  • 5
  • 14
  • You can't do "easily" what you want. Slices and arrays are not 2-dimensional, they are just composed to form a multi-dimensional object. See [How is two dimensional array's memory representation](https://stackoverflow.com/questions/39561140/how-is-two-dimensional-arrays-memory-representation/39561477#39561477). – icza Feb 03 '19 at 21:52
  • What you're talking about is just not something that Go does. `a[0:2]`is a slice consisting of the first three elements of `a` (i.e., a slice of 3 `[]int`s). If you do the same slice operation on that, you just get the same thing, as you saw. You may want to take a look at these explanations of how slices work: https://tour.golang.org/moretypes/7 https://blog.golang.org/go-slices-usage-and-internals – Andy Schweig Feb 03 '19 at 22:04
  • @AndySchweig thanks that's just the explanation I needed. I guess I was naively hoping for a `numpy` type capability – Jonas D Feb 03 '19 at 22:11

1 Answers1

7

You can't do what you want in a single step. Slices and arrays are not 2-dimensional, they are just composed to form a multi-dimensional object. See How is two dimensional array's memory representation

So with a slice expression, you just get a slice that will hold a subset of the "full" rows, and its type will be the same: [][]int. If you slice it again, you just slicing the slice of rows again.

Also note that the higher index in a slice expression is exclusive, so a[0:2] will only have 2 rows, so you should use a[0:3] or simply a[:3] instead.

To get what you want, you have to slice the rows individually like this:

b := a[0:3]
for i := range b {
    b[i] = b[i][0:3]
}
fmt.Println(b)

This will output (try it on the Go Playground):

[[0 1 2] [1 2 3] [2 3 4]]

Or shorter:

b := a[:3]
for i, bi := range b {
    b[i] = bi[:3]
}
icza
  • 389,944
  • 63
  • 907
  • 827