"Go Learning note" 7, slice Slice

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

Sliced slice

    • It is not an array by itself, he points to the underlying array
    • As an alternative to variable-length arrays, you can associate local or all of the underlying array
    • As a reference type
    • You can create directly or get a build from the underlying array
    • Use Len () to get the number of elements, and the cap () to get the capacity
    • General use make () to create
    • If multiple slice point to the same underlying array, one of the value changes affects all

    • Make ([]t,len,cap)

    • Where the cap can be omitted, and the Len value is the same
    • Len represents the number of elements stored, and the CAP represents the capacity
1234
func Main ()make ([]int) fmt. Println (S1)}

Reslice

    • Reslice indexes are subject to slice slices
    • Index can not exceed the capacity cap () value of the slice slice
    • Index out-of-bounds does not cause redistribution of the underlying array but throws an error

Append (Append ())

    • You can append elements to the slice tail
    • One slice can be appended to the other slice tail
    • Returns the original slice if the final length does not exceed the capacity appended to the slice
    • If the capacity of the appended slice is exceeded, the array is reassigned and the original data is copied

Copy

 123456789101112 
 func  main   ()   {s1: = []int  {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }s2: = []int  {11 , 12 ,  }copy  (S1,S2) fmt. Println (S1) //[11 4 5 6 7 8] } 
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.