Golang append slice len and cap

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Statement:

SOURCE slice= src

Add slice = App

Result Slice=tar

When append

Len tar = = Len src + len App

1) If Len (src) + len (APP) <= cap (src) cap tar = Cap (SRC)

2) otherwise

a) len (SRC) + len (APP) > Cap (src) cap tar = Len (src) + len (APP)

b) Cap (SRC) < len (SRC) + len (APP) <=-cap (src) cap tar = Cap (SRC)


    data := make ([]int, 10, 20]     data[0] = 1    data[1] = 2     dataappend := make ([]int, 12, 30)//Modify this len     dataappend[0 ] = 1    dataappend[1] = 2    result :=  Append (data, dataappend)     result[0] = 99    result[11 ] = 98    fmt. Println ("Length:",  len (data),  "Cap:",  cap (data),  ":",  data)      Fmt. Println ("Result length:",  len (Result),  "cap:",  cap (Result),  ":",  result)      fmt. Println ("Length:",  len (dataappend),  "Cap:",  cap (Dataappend),  ":",  dataappend) 



Related Article

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.