範例:https://play.golang.org/p/tTMQ2l8W--B
x := []int{1, 2, 3}
y := []int{4, 5, 6}
fmt.Println(append(x, 4, 5, 6))
fmt.Println(append(x, y...))
輸出結果:
[1 2 3 4 5 6]
[1 2 3 4 5 6]
append 的用法有兩種:
slice = append(slice, 元素1, 元素2)
slice = append(slice, anotherSlice...)
第一種用法的第一個參數為 slice,後面可以添加多個元素。
第二種用法是將兩個 slice 拼接在一起,且需要在第二個 slice 的名稱後面加三個點,
這時候 append 只支持兩個 slice 拼接,不支持任意數量。