Arrays are rarely used directly in Golang, but slicing is a dynamic implementation of array Arrays, so it’s important to understand how arrays are implemented first.

An array of array

Array declaration

An array is a fixed-type, fixed-length sequence. The syntax for arrays is as follows:

Var Variable name [array length] Array element type // var a [2]int

Each element in the array can be accessed by index subscripts ranging from 0 to the array length -1. Len () is the built-in function to get the number of elements in the array (the array length).

An array is initialized with a default value of zero for each element of the element type, or it can be declared as a direct assignment

var b [3]int = [3]int{1, 2, 3}

If you do not know the size of the array when you accidentalize, you can use… The length of the array depends on the number of elements at initialization

c := […] int{1, 2, 3}

Slice slices

Slice a slice is a reference type that refers to an underlying array and is a reference to a contiguous fragment of the array. Note that the terminating index subscript value is not included in the slice.

Slice can grow and shorten dynamically, making it more flexible than arrays of fixed type and length

Sliced declaration

It is common to generate slices from contiguous memory of arrays and slices. The syntax for slicing is as follows:

Slice [start position: end position] // Slice slicing variable

Note: terminating index subscript values are not included in the slice, for example:

Upper limit of slice capacity

The built-in make function is also used to create a Slice. The syntax is as follows:

func make([]Type, len, cap) []Type

Note the capacity upper limit for the third optional parameter.

  • Do not specifyCapacity capThe default length is len.
  • Slices [:cap(slices)] cannot access the contents of the raw array after slices are specified
  • Cap for slicingThe first elementinThe index of the original arraywithOriginal array lengthThe absolute value of the difference between
s := make([]int.5),` s ` sliceLength of5, capacity upper limit5. s2 := s[1:3]
Copy the code

When slicing, Golang will create a new slice s2, and the underlying reference data will not move. S2 slices by changing pointer pointing, length and capacity. This is why the slice performance is very high.

A slice cannot operate beyond the cap because, from a pointer point of view, it is an illegal access.

Slice append element

Golang’s built-in function Append () can dynamically add elements to slices. When the maximum capacity of slices is insufficient to accommodate elements, slices will be automatically expanded and the length of slices will be changed.

In slice expansion, the upper limit of capacity is regularly expanded by 2 times of the upper limit (1, 2, 4, 8) and the previous elements are copied in. Here’s the test:

To append a slice to another slice:

Slice copy

Golang’s built-in function copy() copies array slices using the following syntax:

Copy (number1,number2) // Copy the content of number2 to number1

Note that: If the upper limits of the two slices are different, the array slice with the smaller upper limit will be replicated.