background
This article is for me to readYoung Lin experiment -Golang CollectionThe study notes are similar to the text version of the video. The pictures in the article are also captured from the video, plus some notes of their own. This video is suitable for students who have the Go foundation, so the article is the same! There are incorrect places welcome to correct ah ~
slice
Slice has three parts: 1: where elements are stored 2: how many elements are stored 3: how many elements can be stored
Declare an integer slice var ints []int
Slice elements are stored in a contiguous segment of memory, which is essentially an array
The variable ints consists of three parts: data=nil: the starting address of the underlying array, but so far only the slice structure has been allocated, the underlying array has not been allocated, so nil has zero storage and zero capacity
If you were to define the variable as make, you would not only allocate the three-part structure, but also allocate a chunk of memory as its underlying array. Here, make makes a memory block for inTS that holds five integer elements and initializes them to the default value of 0. Var ints []int = make([]int, 2, 5)
Add element ints = append(ints,1) ints[0] = 1
Stored elements are safe to read and write, but beyond this range, the access is out of bounds and panic occurs
Look at slice as a string, but instead of make, try new.
New a slice variable that allocates the same three-part structure, but does not allocate the underlying array, so data=nil,len=0,cap=0 The return value of new is the starting address of the slice structure
ps := new([]string)
The return value of new is the starting address of the slice structure, so ps is the address, and the slice variable does not have the underlying array (*ps)[0] = “eggo”, such operations are not allowed
So who’s going to assign it the underlying array? Append
Append *ps = append(*ps, “eggo”)
The string type consists of two parts, one is the starting address of the content, pointing to the content of the string, and the other is the length of bytes
Let’s look at the underlying array that is closely related to Slice
Array: Elements of the same type are placed next to each other to store an int. the bottom layer is an int. the bottom layer is a string
Arr := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 0}
We can associate different slices to the same array like this, they will share the underlying array var s1 []int = arr[1:4] The size starts at 1 and ends at 9 elements: data = [1, 2, 3], len = 3, cap = 9
Var s2 []int = arr[7:] the element of s2 starts and ends at index 7: data = [7, 8, 9], Len = 3, cap = 3
Slice accesses and modifies elements of the underlying array and extends the range of reads and writes via append
S2 = append(s2, 10); s2 = append(s2, 10); s2 = append(s2, 10)
How do you know how big it’s going to be and how it’s going to be expanded? That depends on Slice’s expansion rules.
Ints := []int{1,2} capacity before capacity expansion oldCap = 2 ints = append(ints, 3,4,5) capacity after capacity expansion newCap = 5
Capacity expansion rule: Step1: NewCap rules for estimating capacity after expansion: 1. If the capacity before expansion is still less than the minimum required capacity, the estimated capacity is equal to the minimum required capacity. 2. (1) If the number of elements before expansion is less than 1024, it will be directly doubled. (2) If it is greater than or equal to 1024, it will be expanded by a quarter first, that is, to 1.25 times the original expansion
The estimated capacity is simply an estimate of the “number” of elements. How much memory will be required for this number of elements depends on the element type
Step2: newCap how much memory is required for each element Required memory = estimated capacity * element type size
In a programming language, the application for memory allocation is not directly negotiated with the operating system, but with the language’s own memory management module, it will apply for a batch of memory to the operating system in advance, divided into common specifications for management. When we request memory, it will help us match the size to the nearest size that is large enough
Specification: 8,16,32,48
Step3: match the estimated requested memory to an appropriate memory size
In the previous example, if the estimated capacity is 5 or less than 64 bits, you need to apply for 40 bytes to store the expanded underlying array. However, the actual application will match 48 bytes 5 x 8 = 40 bytes
So how many elements can you fit in that amount of memory? In this case, each element is 8 bytes, and you can hold 6 in total