Search code examples
gosliceflatten

How to flatten a 2D slice into 1D slice?


I have

arr := [][]int32 {{1,2,3} ,{4,5,6}, {7,8,9}}

and I want

newArr := []int32 {1,2,3,4,5,6,7,8,9}

In JS I can do

arr1d = [].concat(...arr2d);

as one of many simple ways like this

Is there in Go something like this?


Solution

  • As predicted by Eli Bendersky, from Go 1.22 you have slices.Concat