函数append()是golang用于操作切片的内置函数,先看看函数定义:
// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
// slice = append(slice, elem1, elem2)
// slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
// slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type
函数作用是将单个元素或者元素类型的slice追加到原先slice尾部
If it does not, a new underlying array will be allocated.
如果没有足够的容量(capacity),那么一个新的底层数组会被创建,并且slice的成员变量array会被更新以指向这个新数组。
注意:append返回的值与原先slice共用同一个底层数组