Learning Golang Language (6): Type--slice

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

Learning Golang Language (1): Hello World

Learning Golang Language (2): Variables

Learning Golang Language (3): Type--Boolean and numeric types

Learning Golang Language (4): Type--string

Learning Golang Language (5): Type--array

Learning Golang Language (6): Type--slice

In many scenarios, arrays are not able to meet our needs. When we initially define an array, we do not know the length required by the array. Therefore, we need an array that can be dynamically changed in size (dynamic array)

In the go language, this " dynamic array " becomes a slice (slice).

But actually slice is not really a dynamic array, but a reference type . Slice always points to an underlying Array,slice declaration as an array. But it does not need to declare the length. The length of the slice is variable.

Strictly speaking, the slice has two attribute capacity (capacity) and length , where the capacity >= length.

The declaration of a slice (slice)

Slices can be created in the following ways.

1, first declare a variable is a slice, and then use the built-in function make to initialize the slice.

Here we first use the Make function to define the slice Slice1, at this time the capacity of Slice1 = 5; length = 5; Then use the Make function to define the slice Slice2, this time slice2 capacity = 10, length = 5;

So you can see that there are two ways to define slices using make,

    • Only specify length, at this time the slice length and capacity are the same;

    • Specify both the length and the capacity of the slice.

-------------------- Note --------------------

1, when the capacity is greater than the length, the assignment still needs to note that the largest index is still Len (slice)-1, otherwise the index is reported out of bounds error.

2. Use: = symbol to initialize the data while declaring a slice, as shown below.

Slice: = []byte{' A ', ' B ', ' C ', ' d ', ' e '}

3. Slices can be declared again from an array or an existing slice. Slices through array[i:j] to get the elements of the array index from I to J. Where I is the starting position, J is the place of the sleepover. But does not contain array[j], the length is j-i.

---------------------------------------------

also : Array declarations are required to specify the length of the array or use (...) in square brackets. The symbol automatically calculates the length. In the case of a slice declaration, there are no characters in the square brackets.

The slices also have some simple operations

    • The slice default start position is 0,slice[:n] equivalent to slice[0:n];

    • The second sequence of slices is the length of the array by default, Slice[n:] equivalent to Slice[n:len (slice)];

    • If you get a slice directly from an array, you can slice[:] because the default first sequence is 0, the second sequence is Len (slice), so slice[:] is equivalent to Slice[0:len (slice)].

For example:

Output Result:

Since a slice is a reference type , all other references change that value when the reference changes the value of the element. For example:

Output results



When we change slice1[3] to 100, slice2[0] also becomes 100.

---------------------------------------------

Conceptually, a slice is like a struct, containing three elements:

    • A pointer to the start position of the slice specified in the array;

    • Length, which is the length of the slice, obtained through the built-in function Len;

    • Maximum length, which is the maximum size of the slice, obtained through the built-in function cap.

---------------------------------------------

Important features of slices: variable length , which can be understood by the following examples.

Append append Element

Output results


Here we initialize the slice as capacity = 8; length =4 slices. The first four elements are then assigned a value and the result is output. Furthermore, using Go's built-in function append adds 6 elements to slice.

This time look at the capacity of slice = 16 and length =10 and slice elements. found that the length of the slice did change.

-------------------- Note --------------------

Append when appending an element

    • If the new tile length is less than capacity, the capacity is not changed.

    • If the new tile length will exceed the capacity, go will automatically re-allocate the capacity for the slice. The capacity is twice times the size of the original.

The above example shows: the capacity from the original 8=>16.

--------------------------- ------------------

This article describes adding elements to slices using the Append function, and now introduces a copy function: used to copy elements from one slice to another.

Copy copied elements

Output results


In the example above, we copy the elements of the Slice1 to Slice2 because the length of the Slice2 is 5, so a maximum of 5 elements are copied.

--------------------Summary--- -----------------

An array declaration is required to specify the length of the array or use (...) in square brackets. The symbol automatically calculates the length. In the case of a slice declaration, there are no characters in the square brackets.

Because the array length is fixed, the slice length is variable.

---------------------------------------------

Welcome to pay attention to code surgery! Learn Golang language together.


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.