This is a creation in Article, where the information may have evolved or changed.
If the go language array is a static-length array, then the slice (slice) is a dynamic-length array
First, create slices based on arrays
1, there is an integer array intarr: = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, then the following slice is the array slice
var slice []int = Intarr[3:7]
You can see that the slice starts reading data from the 4th element of the array, up to the 8th element (but not 8th). Remember that the programmer's count starts at 0.
2. What should I do if I read only the first 4 elements of the intarr array? You must be able to think of the smart
var slice [] int = Intarr[0:4]
Or
Slice: = Intarr[0:4]
3. Is there a more concise representation? That's for sure, go knows programmers.
Slice: = Intarr[:4], which is equivalent to slice: = Intarr[0:4]
4, which read all the data after the 4th element of the Intarr array (containing the 4th element)?
Slice: = Intarr[3:10]
Of course, you can also say the following
Slice: = Intarr[3:len (intarr)]
Or
Slice: = intarr[3:]
Second, the relationship between the slice created by the array and the array
In the example above, we created the slice slice based on the array intarr, so what does slice have to do with intarr? It can be simply understood that the slice is based on an array that adds some administrative functionality, and the very type of array in C + + is related to std::vector.
Intarr: = [10]int{1,2,3,4,5,6,7,8,9,10} //define array Intarr
var slice []int = Intarr[3:7] Listen and listen//create slices based on intarr listen
Fmt. Printf ("Intarr's address is:%p\n", &intarr) listen
Fmt. Printf ("Slice's address is:%p\n", &slice)
Fmt. Printf ("The 4th element of the Intarr address is:%p\n", &intarr[3])
Fmt. Printf ("The 1th element of the slice address is:%p\n", &slice[0])
From the running results of the program you can see that the slices slice and arrays intarr have no relation to each other and are different intrinsic addresses; But interestingly, the first element of the slice slice is the address of the 4th element of the array intarr, which is not a coincidence, but the essence of the slice.
So some people also say that slices are pointers to arrays, and changes to the slice elements affect the values of the array elements, which can be verified by overwriting the above example:
Intarr: = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Slice: = intarr[3:]
Slice[0] = +//Change the 1th element value of the slice to
Fmt. Println ("slice=", Slice)
Fmt. Println ("intarr=", Intarr)
Creating slices based on slices
Can I create slices based on slices? The answer is yes, it uses the same method as creating slices based on arrays
Intarr: = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Slice: = intarr[3:]
Reslice: = slice[1:]
Fmt. Println (Reslice)
Since slice is a slice created from an array intarr, it starts with the 4th value of Intarr, so the value of slice is [4,5,6,7,8,9,10], and Reslice is a slice created based on the slice slice, which starts with the 2nd value of slice, So the value of Reslice is [5,6,7,8,9,10]
Iv. creating slices directly
Instead of having to create slices based on arrays or slices, the go language is a thoughtful way for programmers to provide the make () function in the form slice: = make ([] type, number, capacity), for example:
Slice: = Make ([]int, 3, 10)
1. What is the meaning of number and capacity respectively?
number is a slice element, after creating a slice slice, there are several elements; capacity represents the capacity of the slice, and after the slice slice is created, the storage space for 10 elements is reserved. Run the above example to understand:
From the results, the number of slices slice elements is 3, and the element default padding is 0, because the slice slice capacity is 10, so you can also append 7 elements to the slice.
2, Len () and Cap ()
The slices have the Len () and Cap () functions, which are used to obtain the number of elements and the capacity values of the slices, for example:
Slice: = Make ([]int, 3, 10)
Num: = Len (slice)
Capacity: = Cap (slice)
Fmt. Println ("Number of slices slice elements:", num, ", Capacity:", capacity)
3, can omit the capacity?
The answer is OK, so you can declare the slice like this: slice: = make ([]int,3)
At this point it has the same capacity as the number of elements, which is 3
4, can the number of elements and capacity are omitted into slice: = make ([]int) feasible?
The answer is no, at this point the go language dizzy, it is thinking that the programmer let me put all the memory occupied? Throws missing Len argument to make ([]int) error message
5, only type and capacity, omit the number of elements, similar to slice: = make ([]int,,10)
The answer is no, but can be declared after direct initialization, i.e. slice: = []int{1,2,3,4,5,6}
6, how to add elements?
Slice: = Make ([]int, 3, 10)
Fmt. Println ("Number of slices slice elements:", Len (Slice), ", Capacity:", cap (Slice), ", slice=", slice)
You can append this:
Slice = Append (slice, 4)
Slice = Append (slice, 5)
You can also append this:
Slice = append (slice, 6, 7, 8)
You can even append a slice, but you must add it later ... :
Arr: = [2]int{9, 10}
Slice = append (slice, arr ...)
After the program runs, the result is as follows:
Does the slice regenerate after the slice appends the element?
1. Additional Instructions
Slice: = Make ([]int, 3, Ten) //Declare a slice
Fmt. Printf ("Address of original Slice%p\n", &slice)
Fmt. Printf ("Address of the first element of the original slice%p\n", &slice[0])
Slice = Append (slice, 4) Listen //Append an element
Fmt. Printf ("Address of new slice%p\n", &slice)
Fmt. Printf ("Address of the first element of the new slice%p\n", &slice[0])
As you can see from the running results, the address of the slice element does not change after appending an element, but must return a value to slice when applied, that is, the following program is wrong:
Slice: = Make ([]int, 3, 10) listen
Append (Slice, 4)//Must return slice
Fmt. Printf ("%p\n", &slice)
2, append more than the capacity size
Slice: = Make ([]int, 3, 10) Listen, listen, listen, listen, listen. //Slice capacity
Fmt. Printf ("%p\n", &slice) Listen, listen, listen, hear, listen, and/ or print the slice address .
Fmt. Println ("Len:", Len (Slice), "cap:", cap (slice)) listen //Print the number of slice elements and capacity
Slice = Append (Slice, 4, 5, 6, 7, 8, 9, 10, 11) Listen //slice append element
Fmt. Printf ("%p\n", &slice) Listen, listen, listen, hear, listen, and/ or print the slice address .
Fmt. Println ("Len:", Len (Slice), "cap:", cap (slice)) listen //Print the number of slice elements and capacity
In this example, a slice with a capacity of 10 is declared at the beginning, and when an element is appended to the slice, the number of elements exceeds the capacity, and the slice automatically expands capacity one time to 20, which is similar to the Java collection
This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1616987