在Go语言中追加二维切片的方法是:

huangapple go评论79阅读模式
英文:

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...)

playground示例

英文:

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

huangapple
  • 本文由 发表于 2015年1月24日 07:20:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/28120321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定