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?
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?
Go has strings.Join and bytes.Join, but no generic functionality to join/concat a slice. It's possible that once generics are introduced into the language such functionality will be added to the standard library.
In the meantime, doing this with a loop is clear and concise enough.
var newArr []int32
for _, a := range arr {
newArr = append(newArr, a...)
}
I am not going to add anything new since my answer is a copy of Eli's code. But I should note that Go propose is to be simpler than C/C++, but not as simple as Python/JS. It's not hiding a cost "magic" calls.