Golang Note--slice

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

First, the definition of a slice

We can initialize a new slice from an array or a slice, or we can initialize a slice of all elements as the default 0 value directly through make.

//1. Initializing slices by arrayArr: = [...]int{1,2,3,4,5} Slice1:= arr[:]//[1,2,3,4,5]Slice2: = arr[2:4]//[3,4]Slice3: = arr[:4]//[1,2,3,4]Slice4: = arr[2:]//[3,4,5]//2. Initialize slices with slicesSlice5: = slice1[3:]//[4,5]//3, direct initialization. You can initialize by make, with the format make ([]int, Len, cap), all elements initialized to the default value of 0, or directly initialized. Slice6: = make ([]int,3,6)//[0,0,0]Slice7: = []int{1,2,3,4,5}//[1,2,3,4,5]

Second, the characteristics of the slice

Slice a bit like the vector in C + +, are variable-length arrays, with the concept of capacity, but there are many differences, such as Slice is based on an array (the array is called the Base series), the length of the slice is removed from the base series of the number of elements, The capacity is the difference between the maximum index of the base series and the smallest index of the removed element. Array is a value type, but slice is a reference type.

Func func1 (S []int) {s[0] *=Ten //the value of the original slice is modified}func Func2 (s []int) {     for_, V: = range S {//the value of the original slice is not modified because the for ... range is read-onlyV *=Ten}}func func3 (s []int) {     forI: =0; I < Len (s); i++ {//the value of the original slice is modifiedS[i] *=Ten}}func Main () {s:= []int{1,2,3,4,5}    //func1 (s)//10,2,3,4,5,//Func2 (s)//1,2,3,4,5,//func3 (s)//10,20,30,40,50,     for_, V: =range S {println (V,",")    }}

Iii. operation of slices

Slicing is more complex than arrays, because slices are variable-length and have a concept of capacity. When the tile capacity is not enough, it expands at twice times the rate.

1. Get the slice length: Len (Slice)

2. Get slicing Capacity: cap (slice)

3. Append elements for slices: Slicename = append (Slicename, element1, Element2,...)

The function is a variable parameter function, and the variable parameter part can also be passed to another slice, in the form slicename = Append (Slicename, slicename2 ...), you can refer to the use of the variable parameter function. In fact, I have been wondering why to assign to the original slice, in fact, because, but append function call, if the tile capacity is not enough, will create a new tile variable, this should be why to assign to the original slice why it.

4. The slice does not provide a way to delete the element, but it can be achieved by obtaining a child slice, but it cannot be directly removed from the element, nor can it be inserted from the middle.

5. Copy of Slice: Copy (Slice1, Slice2)

If the index position passed in is greater than or equal to the length of the slice,

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.