1

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?

Flimzy
  • 68,325
  • 15
  • 126
  • 165

2 Answers2

2

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...)
}
Eli Bendersky
  • 248,051
  • 86
  • 340
  • 401
-1

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.

Oleg Butuzov
  • 4,229
  • 2
  • 20
  • 31