Creation method of golang slice

Time:2022-1-2

When creating a new slice, you will first create an array with a length of len and open up an additional space with a length of cap. Therefore, adding elements in the cap range class will not change the starting address of the array, otherwise a new array will be created, that is, the starting position will change

Array creation

code

intArr:=[...]int{1,2,3,4,5,6,7,9}
//Method 1 specifies the start and end
s:=intArr[1:3]
//Method Er specify start and end + capacity
s:=intArr[1:3:6]

Slice after creation

[a: b :c]

a: Starting position

b: End position of intercepted data (default to end)

c: Capacity position after interception (default to end)

Len is the length of the intercepted element, that is: (3 – 1 = 2)

Cap is the length from the intercepted starting element to the last element of the element group, that is:

(original array length: 8 – starting element 1 = 7)

Bottom array: from the intercepted starting element of the original array to the last element of the prime group

在这里插入图片描述

Make create

code


s:=make([]int,10,20)

Slice after creation

Len is: 10

Cap: 20 (specified when creating)

Underlying array: an array with a length of 10

Slice expansion

No new underlying array will be created within the scope of cap

If the cap is exceeded, a new array will be created and new cap and Len will be generated

Supplement: precautions when creating slice using array in golang

In golang, slice is a slice of a fixed length array, and its bottom layer is realized by a pointer to the value space.

In golang, when an array is used to create slice, for example:

var array [5]int = [5]int{1,2,3,4,5}
//Cut out the four values in the array and create a slice
myslice := array[0:4]

The value of print array is:

1, 2, 3, 4, 5

The value of printing mysqlice is:

1, 2, 3, 4

Now let’s append myslice


myslice = append(myslice,100)

The value of print array is:

1, 2, 3, 4, 100

The value of printing mysqlice is:

1, 2, 3, 4, 100

Now let’s modify myslice again


myslice[0] = 50

The value of print array is:

50, 2, 3, 4, 100

The value of printing mysqlice is:

50, 2, 3, 4, 100

When the length of slice does not exceed the length of the array to be created, slice still refers to the array used during creation.

But!

Now the length of myslice is consistent with the length of the array used to create slice

We then perform the append operation on myslice to make the length of myslice greater than the length of the array used during creation


myslice = append(myslice,200)

The value of print array is:

50, 2, 3, 4, 100

The value of printing mysqlice is:

50, 2, 3, 4, 100, 200

Isn’t myslice associated with arr through pointers? Why does this situation not change the value of arr?

Because when myslice exceeds the length of array, the go language will implicitly copy the array and make the pointer inside myslice point to the new value again. Therefore, all expected operations to modify the value of array will not take effect!

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.

Recommended Today

Explanation of websocket heartbeat reconnection

Recently, websocket has been used in the development of applet. The applet provides corresponding native API, which is different from H5 API. Therefore, some mature class libraries of popular H5 are difficult to use, and the native API has some defects, so it implements a set of heartbeat reconnection mechanism by itself. First, let’s briefly […]