Golang data Two (slice)

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

In the project development process, more scenarios are required a data storage structure that can be dynamically updated in length, the slice itself is not a dynamic array or an array pointer, he refers to the underlying array by pointers, and sets the relevant properties to limit the data read and write operations within the specified area. Like what:

/runtime/slice.gotype Listen to slice listen to the struct Listen {array listen to unsafe. Pointerlen listen to listen to Intcap listen to int}

Slice initialization

There are two basic ways of initializing a slice:

Slices can be initialized with the built-in make function, len=cap when initialized, generally omitting the cap parameter when used, the default and Len parameters are the same, when appending elements, if the capacity cap is not sufficient, it will dynamically expand by twice times Len.

Initializes the slice with an array to start and end the index position to determine the final referenced array fragment.

Make ([]t, listen to Len, listen to cap) listen to//t is the type of data that is sliced, Len says Length,cap says capacity{listen to listen: = Listen to make ([]int,5) listen and listen to the//len: Listen to the 5 listen to the CAP: Listen to listen to listening to]int,5,10 listen to listen to listen to hear: = Listen to//len: Listen to the 5 listen to the CAP: listen to 10 listen to listen to listening to hear: = Listen to []int{1,2,3}} Listen to listen to hear {listen listening to arr: = hear [...] int{0,1,2,3,4,5,6,7,8,9} Listen to listen to hear S1 listen: = Listen to arr[:] Listen to listen to listen to S2 listen: = Listen to Arr[2:5] listen to listen to listen to S3 listen to listen to Arr[2:5:7 listen to listening to hear, listen to: = Listen to the S4:] Listen to listen to listen to the arr[4 listen to: = Listen to S5 : 4] Listen and listen to S6 listen: = Listen to the Arr[:4:6] listen and listen to the FMT. Println ("S1: Listen", S1, listen to Len (S1), Cap (S1)) listen and listen to the FMT. Println ("S2: Listen", S2, listen to Len (S2), Cap (S2)) Listen and listen to the FMT. Println ("S3: Listen", S3, listen to Len (S3), Cap (S3)) Listen and listen to the FMT. Println ("S4: Listen", S4, listen to Len (S4), Cap (S4)) Listen and listen to the FMT. Println ("S5: Listen", S5, listen to Len (S5), Cap (S5)) listen and listen to the FMT. Println ("S6: Listen", S6, listen to Len (S6), Cap (S6))} output: S1: Listen [0 listen to 1 listen to 2 listen to 3 Listen 4 Listen 5 Listen 6 Listen 7 Listen 8 listen to 9] listen to the 10S2: Listen [10 Listen 2 Listen 3] Listen to 4 listen to 8S3: Listen to [3 listen to 2 listen to 3] 4 Listen 5 Listen 6 Listen 7 Listen 8 Listen 9] Listen 6 listen to 6S5: Listen to [0 Listen 1 Listen 2 Listen 3] Listen to 4 listen to 10S6: Listen [0 Listen 1 Listen 2 Listen 3] Listen 4 Listen 6

The previous example shows that the CAP is the true length of the array fragment referenced by the slice, and Len is the maximum subscript (index) value plus 1 that has been assigned a value.

Note the differences between the following two types of initialization:

{Listen and listen to Var listen to a listen to []int listen to listen to B listen to: = Listen to []int{} Listen listen to the FMT. PRINTLN (A==nil,b==nil) Listen and listen to the FMT. Printf ("A: listen% #v \ n", listen (*reflect. Sliceheader) (unsafe. Pointer (&a)) listen and listen to the FMT. Printf ("B: listen% #v \ n", listen (*reflect. Sliceheader) (unsafe. Pointer (&B)) listen and listen to the FMT. Printf ("A listen to size listen to%d\n", listen to unsafe. Sizeof (a)) listen and listen to the FMT. Printf ("B Listen to size listen to%d\n", listen to unsafe.) Sizeof (b))} Output: True listen Falsea: Listen to &reflect. sliceheader{data:0x0, listen to len:0, listen to Cap:0}b: Listen to &reflect. Sliceheader{data:0x5168b0, listen to len:0, listen to cap:0} A listen to size listen to 24b listen to size listen to 24 description: 1. Listen to the internal pointer of variable B is assigned, even if the pointer points to runtime.zerobase, but it still completes the initialization operation 2. Listen to variable a represents an uninitialized tile object, and the slice itself will still allocate the required memory

Logical operators are not supported between slices and can only be judged as nil, such as:

{Listen and listen to Var listen to a listen to []int listen to listen to B listen to: = Listen to []int{} Listen listen to the FMT. PRINTLN (a==b) listen to//invalid listen to Operation: Listen to a listen = = Listen to B listen to (slice listen to can listen only listen to listen to compared listen to nil)}

Reslice

In the original slice based on the new slice, the new slice still point to the original underlying array, the newly created slice can not exceed the original slice

capacity, but is not limited by its length, and if you modify the value of the new slice, it affects all associated slices, such as:

Listen to listen to hear: = Listen to []string{"a", "B", "C", "D", "E", "F", "G"} Listen to listen to hear the S1 listen to: Listen to listen to hear listening and listening listen to listen to hear listening and listening listen to listen to//b,c listen to listen to the FMT. Println (S1, listen to Len (S1), Cap (S1)) listen to listen to the s1_1 listen to listen to the s1[2:5] listen to listen to hear listen to listen to listen to the//c,d,e hear and hear the FMT. Println (S1_1, Hear Len (s1_1), Cap (s1_1))} output: [b listen C] Listen to 2 listen 6[d listen E F] Listen 3 Listen 4

Append

Append data to the end of the slice to return the new slice object; The data is appended to the original underlying array, and if the cap limit is exceeded, the new tile object is reassigned to the array, and the newly allocated array cap is twice times the original array cap, for example:

Listen to listen to s listen: = Listen to make ([]int,0,5) listen to listen to hear s listen = listen to append (s Listen, listen to 1) listen to listen to hear s listen = listen to append (s listen, listen to 2,3,4,5) Listen listen to the FMT. Printf ("%p, listen to%v, listen to%d\n", listen to S,s, listen to cap (s)) Listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, Listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, hear, hear, listen, hear, hear, hear, listen, hear, hear, listen. Listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, append. Listen 6 Listen and listen//redistribute memory listen and listen to the FMT. Printf ("%p, listen to%v, listen to%d\n", listen to S, listen s, listen to cap (s))} output: 0xc420010210, listen [1 Listen 2 Listen 3 Listen 4 listen to 5], listen to 50xc4200140a0, listen [1 Listen 2 Listen 3 Listen 4 Listen 5 Listen 6], listen to 10

If you append data to a nil slice, it will reallocate memory and data replication at high frequency, such as:

{Listen and listen to the Var listen to listen to []int listen and listen to the FMT. Printf ("%p, listen to%v, listen to%d\n", listen to S,s, listen to the cap (s)) listen to hear for listening to the i:= listen to 0; listen to listen to < listen to the 10;i++{listen to listen to listen to hear listen to the append (s, listen i) listen to hear listen to listen listen to listen to the FMT. Printf ("%p, listen to%v, listen to%d\n", listen to S, listen to S, listen to the cap (s)) Listen to Listen}}

So in order to avoid the frequent resource overhead in the program running, it is recommended to set aside enough space in some scenarios.

Copy

When data is copied between two slice, it is allowed to point to the same underlying array and allow the target interval to overlap. The length of the final copy is based on a shorter slice length (len), such as:

{Listen and listen to S1 listen: = Listen to []int{0, listen to 1, listen to 2, listen to 3, listen to 4, listen to 5 listen, 6} Listen and listen to S2 listen: = Listen to []int{7, listen to 8 listen, 9} Listen and listen to copy (S1,S2) listen to the FMT. Println (S1,len (S1), Cap (S1)) listen to listen to S1 listen to []int{0, listen to 1, listen to 2, listen to 3, listen to 4, listen to 5 listen, 6} Listen and listen to the S2 listen = listen []int{7, listen to 8 listen, 9} Listen and listen to copy (S2,S1) listen to the FMT. Println (S2,len (S2), Cap (S2))}

Can you copy data between the same slices?

In the course of project development, if slice references a small fragment in a large array for a long time, it is recommended to create a separate slice and copy the required data so that the original array memory can be retrieved by GC in time.

This article from "learned in the text, about the ceremony" blog, reproduced please contact the author!

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.