Use of Golang Silce/append

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

Initialization

Slices can be initialized by an array, or by using the built-in function make (). Len=cap at initialization time, if the capacity cap is not sufficient when appending elements to see sample code at twice times the size of Len, run the sample code online

S: =[] int {A-i}

The direct initialization of the slice, [] means the slice type, and the {-i} initialization value is in turn a. Its cap=len=3

S: = arr[:]

Initializes the slice S, which is a reference to the array arr

S: = Arr[startindex:endindex]

Create an element in Arr from subscript startindex to endIndex-1 as a new slice

S: = Arr[startindex:]

The default endindex will represent the last element to arr

S: = Arr[:endindex]

The default startindex will represent the start of the first element in arr

S1: = S[startindex:endindex]

Initializing slices by slicing s S1

S: =make ([]int,len,cap)

Initialize slice s,[]int with built-in function make () to identify slices whose element type is int

Assignment and use

A slice is a reference type and you need to be aware of its actions when you use it. To view the sample code, run the sample code slice online via the built-in function append (slice []type,elems ... Type) appends an element, Elems can be a row of type data, or it can be slice because one of the elements is appended, so if you append one slice to another slice you need to bring "...", This means that the elements in the slice are appended to the other slice in turn. In addition, the subscript cannot exceed the Len size when the element is accessed by subscript, as if the subscript of the array cannot exceed the Len range.

S: =append (s,1,2,3,4)

S: =append (S,s1 ...)


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.