Golang (Go language) Append of built-in functions

Source: Internet
Author: User
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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.