Go language (Golang) slicing slice

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

Slice slice is the reference type Len () function Gets the number of elements of the CAP () gets the capacity of the array

1. Means of declaration
[PHP]
(1) var a []int differs from array is that he does not declare length
(2) S2: = Make ([]int, 3, 10)//type of element, number of elements, capacity of element
Fmt. Println (len (S2), Cap (S2)) The number and capacity of output elements
[/php]
2. Convert arrays into slices
[PHP]
A: = [10]int{}
Fmt. Println (a)
S1: = A[:10]//Take the first 10 elements [5:] Take 5-The last element
Fmt. Println (S1)
[/php]
3.slice Test
[PHP]
A: = []byte{' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' h '}
SA: = A[2:5]
Fmt. Println (String (SA))
SD1: = A[3:5]
Fmt. Println (String (SD1))//See effect
[/php]
What we see is that slice_a point to Array_ori is actually pointing from C to K we use FMT. Println (Cap (slice_a)) The result is definitely not 3.

Try it on your own.
[PHP]
A: = []byte{' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' h '}
SA: = A[2:5]
Fmt. Println (String (SA))
S: = Sa[1:3]
Fmt. Println (string (s))
S2: = Sa[3:5]
Fmt. Println (String (s2))
[/php]
Usage of 4.Apppend
When using append, we append the element to the end of the slice, if we append to the slice capacity, we will find that the memory address is not changed, if we append more than the capacity, the memory address will change
[PHP]
A: = Make ([]int, 3, 6)
Fmt. Printf ("%p", a)
A = Append (A, 1, 2, 3)
Fmt. Printf ("%v%p\n", A, a)
A = Append (A, 1, 2, 3)
Fmt. Printf ("%v%p\n", A, a)
[/php]
Run as

Slice is a pointer to the underlying array, and if more than one slice points to the same time, one of them changes and the other changes. Try the bottom one.
[PHP]
A: = []int{1, 2, 3, 4, 5}
S1: = A[2:5]
S2: = A[1:3]
Fmt. Println (S1, S2)
S1[0] = 9
Fmt. Println (S1, S2)
[/php]
When the append appended element in the slice exceeds the pointed capacity, it points back to a new underlying array, so a change to the underlying array does not lead to other changes, try the code below
[PHP]
A: = []int{1, 2, 3, 4, 5}
S1: = A[2:5]
S2: = A[1:3]
Fmt. Println (S1, S2)
S2 = append (s2, 1, 2, 2, 3, 3, 4, 5)
S1[0] = 9
Fmt. Println (S1, S2)
[/php]
5.copy
This is a copy of the function, the code below is copied from S2 to S1 and we will see the result is [7 8 9 4 5]
If it is copy (S2,S1) we see the result [1 2 3]
[PHP]
S1: = []int{1, 2, 3, 4, 5}
S2: = []int{7, 8, 9}
Copy (S1, S2)
Fmt. Println (S1)
[/php]

Without permission, do not reprint this site any article: Micro Network Emoji language (golang) slices slice

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.