This is a creation in Article, where the information may have evolved or changed.
A slice is a variable-length sequence of elements (an array is immutable), and each element has the same type. Slice type writing []t, where T represents the type of element in slice; slice and arrays are similar, but not specified in length.
The connection between the array and the slice is very tight. Slice is a very lightweight data structure, which is a reference type, pointing to an array of the underlying, which is known as the underlying array of slice, slice can access a subsequence of the underlying array, or it can access the entire array. A slice consists of three parts: pointer, length, capacity, pointer to the address of the underlying array element corresponding to the first element in slice, because slice does not necessarily start with the first element of the array, so the first element in slice is not necessarily the first element in the array. Length corresponds to the number of elements in the slice, the length cannot exceed the capacity; the capacity is typically from the first element in the slice to the beginning of the underlying array, to the length of the end of the underlying array. The built-in function Len and the CAP return the length and capacity of a slice, respectively.
Multiple slice can share the underlying array, and even the underlying array portions of their references can overlap. Figure 4.1 shows an array whose elements are string names for each month, and two slice, which overlap the underlying array. Array definition:
Months: = [...] String{1: "January",/* ... */, "December"}
Here January is months[1], December is months[12]. Typically, the first element index of an array starts at 0, but the month is typically from January to December, so when declaring an array, we skip the No. 0 element, where the No. 0 element is initialized by default to "" (an empty string).
The following section describes the slicing operation S[i:j], here 0≤i≤j≤cap (s), which creates a new slice that refers to the word sequence from element I to the j-1 element in S, and the new slice has j-i elements. If the subscript i:s[:j] is omitted, it actually represents S[0:J], if the subscript j:s[i is omitted:], it represents S[0:len (s)]. Therefore MONTHS[1:13] operations will refer to all months, and months[1:] operations are equivalent. Months[:] refers to the entire array. The following shows the second quarter and the summer of the North respectively:
Q2: = Months[4:7]
Summer: = Months[6:9]
Fmt. PRINTLN (Q2)//["April" "may" "June"]
Fmt. PRINTLN (Summer)//["June" "July" "August"]
Two slice have overlapping parts: June, here is a test that contains the same month (performance is not high):
For _, S: = Range Summer {
For _, Q: = Range Q2 {
if s = = q {
Fmt. Printf ("%s appears in both\n", s)
}
}
}
If the subscript of the slice operation exceeds the CAP (s) will result in panic, if more than Len (s) means that the extension slice (cannot exceed the cap (s)), because the length of the new slice will become larger:
Fmt. Println (summer[:20])//Panic:out of range
Endlesssummer: = Summer[:5]//expansion within capacity allowed summer
Fmt. PRINTLN (Endlesssummer)//"[June July August September October]"
In addition, the slice operation of the string is very similar to the []byte slice operation. They all behave as x[m:n], and all return the subsequence of the original sequence, and the underlying array is also the original sequence, so the slice operation is a constant-level time complexity. If the target of X[m:n] is a string, a substring is generated, and if the target is []byte, a new []byte] is generated.
Since the slice reference contains pointers to the underlying array, after passing slice to the function, the function can internally make changes to the underlying array of slice. In other words, copying slice is creating a new reference to the underlying array at a very low cost (actually copying a struct containing three fields). The following reverse function reverses the []int type of slice in place (no memory allocation), and it can be used for slice of any length: