The understanding of Go language slice

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

Golang Slice

Yongsean author

2017.02.17 00:07 Open App

Create slices, len, cap, append

B: = make ([]int, 5)

println (Len (b), Cap (b))//output is: 5, 5

Fmt. PRINTLN (b)//output is: [0 0 0 0 0]

The code above is a slice that generates a default of 5 0 values, and the output below is another

B: = make ([]int, 0, 5)

println (Len (b), Cap (b))//output is: 0, 5

Fmt. PRINTLN (b)//The result of the output is: []

The code above is to generate a cap length of 5, the actual use of a slice length of 0, within the specified cap for the append operation, there is no memory copy expansion operation.

B: = make ([]int, 0, 2)

Fmt. Printf ("%d,%d,%p\n", Len (b), Cap (b), B)//0, 2, 0xc420012190

b = Append (b, 1)

b = Append (b, 2)

Fmt. Printf ("%d,%d,%p\n", Len (b), Cap (b), B)//2, 2, 0xc420012190

b = Append (b, 3)

Fmt. Printf ("%d,%d,%p\n", Len (b), Cap (b), B)//3, 4, 0xc420012198

The last line of output shows that the memory address is not the same as the previous 2 output, and the CAP value is also on the original basis, doubled, equivalent to do the following:

Mock append

TMP: = make ([]int, 0, Cap (b) * 2)//is double the current Cap value

Copy operation skipped ...

b = tmp

Copy

Normal Slice copy operation

A: = Make ([]int, 5)

A[0] = 1

A[1] = 2

Fmt. Println (a)//[1 2 0 0 0]

B: = make ([]int, 5)

B[0] = 11

B[1] = 22

B[2] = 33

B[3] = 44

B[4] = 55

Fmt. PRINTLN (b)//[11 22 33 44 55]

Copy (b, a)

Fmt. PRINTLN (b)//[1 2 0 0 0]

Another effect

A: = Make ([]int, 5)

A[0] = 1

A[1] = 2

Fmt. Println (a)//[1 2 0 0 0]

B: = make ([]int, 0, 5)//Len (b)!=cap (b)

b = Append (b, 11)

b = Append (b, 22)

Fmt. PRINTLN (b)//[11, 22]

Copy (b, a)

Fmt. PRINTLN (b)//[1 2]

The output of the third line, just [1 2], not [1 2 0 0 0], is the corresponding subscript copy operation for the element in the B-Slice Len (b) Length, If Len (b) ==0, the output is []. This is the place to be careful, the old driver will be a careless mistake, do not know people that is more difficult to say.

Slices of a slice

A: = Make ([]int, 5)

A[0] = 1

A[1] = 2

A[2] = 3

A[3] = 4

A[4] = 5

Raw Data output

Fmt. Printf ("%v,%p\n", A, a)//[1 2 3 4 5], 0xc4200141b0

Aslice1: = a[1:]

First set of outputs

Fmt. Printf ("%v,%p\n", Aslice1, Aslice1)//[2 3 4 5], 0XC4200141B8

Fmt. Println (Len (Aslice1), Cap (ASLICE1))//4 4

Aslice2: = A[1:3]

Second set of outputs

Fmt. Printf ("%v,%p\n", Aslice2, Aslice2)//[2 3], 0XC4200141B8

Fmt. Println (Len (aslice2), Cap (ASLICE2))//2 4

Aslice3: = A[:3]

Third set of outputs

Fmt. Printf ("%v,%p\n", Aslice3, Aslice3)//[1 2 3], 0xc4200141b0

Fmt. Println (Len (aslice3), Cap (ASLICE3))//3 5

Above a few sets of output, looking at nothing, look closely or there is noteworthy

Len and cap values are not the same for each group

The memory address of the slice is not exactly the same

Len's value is well understood, without objection.

The value of the CAP:

In the first set of outputs, it is 4, which is the number of the first address of the new slice to the end of the original slice.

is also 4 in the second set of outputs, which is the number of the first address of the new slice to the end of the original slice

In the third set of outputs is 5, the rationale above

Memory Address:

The address of the original data output is the same as the address of the third set of outputs

The address of the first set of outputs is the same as the address of the second set of outputs

The reason for this output is that the first address of the slice is the same as the point. On a 64-bit operating system, the int type is 8 bytes, and the second set of outputs has a value of 8 more than the address of the third set of outputs. If a new slice is defined as follows:

Aslice4: = a[2:]

Fmt. Println ("%p\n", Aslice4)//0XC4200141C0

The output is: 0xc4200141c0, the result of 0xc4200141b8+8

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.