英文:
Appending 2 dimensional slices in Go
问题
我在Go程序中有几个二维切片,我想将它们连接在一起。
然而,append()
函数不接受这种类型。
cannot use myArray (type [][]string) as type []string in append
在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?
答案1
得分: 12
使用...
将第二个切片作为可变参数传递给append
函数。例如:
a := [][]string{{"a", "b"}, {"c", "d"}}
b := [][]string{{"1", "2"}, {"3", "4"}}
a = append(a, b...)
英文:
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...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论