This is a creation in Article, where the information may have evolved or changed.
- Append is primarily used to append elements to a slice (slice)
- If the tile storage space (CAP) is sufficient, it is appended directly, the length (len) becomes longer, and if there is insufficient space, the memory is re-opened and the previous element is copied with the new element.
- The first parameter is a slice, followed by a variable parameter that stores the element type for the slice
Basic usage:
Slice:=Append([]int{1,2,3},4,5,6)fmt.Println(slice) //[1 2 3 4 5 6]
- The second parameter can also directly write another slice, appending all of its element copies to the first slice. It is important to note that the parameters of this usage function can only receive two slice and add three points to the end
Slice:=Append([]int{1,2,3},[]int{4,5,6}...)fmt.Println(slice) //[1 2 3 4 5 6]
- There is also a special use of the string as a []byte type as the second parameter passed in
bytes := append([]byte("hello"),"world"...)
- Append function return value must have a variable to receive, otherwise the compiler will error, for specific reasons please refer to: http://blog.csdn.net/qq245671051/article/details/50722823