Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending 2 dimensional slices in Go

I have a couple 2 dimensional slices in my Go program and I want to join them together.

However, append() doesn't take this type.

cannot use myArray (type [][]string) as type []string in append

How do you append multi-dimensional slices using Go in an idiomatic way?

like image 738
tyler Avatar asked Dec 14 '22 17:12

tyler


1 Answers

Use ... to pass the second slice as variadic parameters to append. For example:

a := [][]string{{"a", "b"}, {"c", "d"}}
b := [][]string{{"1", "2"}, {"3", "4"}}
a = append(a, b...)

playground example

like image 150
Bayta Darell Avatar answered Dec 17 '22 06:12

Bayta Darell