Go编程语言规范
附加并复制切片
可变参数函数
append将零个或多个值附加x到stypeS(必须是切片类型),并返回结果切片(也是type)S。值x将传递到类型为的参数,...T
其中T是的元素类型,S并且适用各自的参数传递规则。append(s S, x ...T) S // T is the element type of S将
...参数传递给参数如果最终参数可分配给切片类型
[]T,...T则在参数后跟时可以将其作为参数的值原样传递...。
您需要使用
[]T...最后一个参数。
在您的示例中,使用最终参数slice type
[]byte,参数后跟
...,
package mainimport "fmt"func main() { one := make([]byte, 2) two := make([]byte, 2) one[0] = 0x00 one[1] = 0x01 two[0] = 0x02 two[1] = 0x03 fmt.Println(append(one[:], two[:]...)) three := []byte{0, 1} four := []byte{2, 3} five := append(three, four...) fmt.Println(five)}游乐场:https :
//play.golang.org/p/2jjXDc8_SWT
输出:
[0 1 2 3][0 1 2 3]


![如何使用带有两个[] byte切片或数组的Go append? 如何使用带有两个[] byte切片或数组的Go append?](http://www.mshxw.com/aiimages/31/471094.png)
