This is a creation in Article, where the information may have evolved or changed.
The knowledge of variables is described earlier, and there is no more introduction to other data types, (and C is almost), so the focus is on the next slice.
First, the slice is a reference type, here are two concepts: value type, construction type and reference type
1. Value type: is a data type represented by the actual value of the type. If a variable is assigned a value type, the variable is appended with a new copy of the value, and the value types of the Go language include Boolean, Integer, float, and plural.
2. Construction type: Similar to C, including arrays, structs, and strings
3. Reference type: The data type represented by the actual value reference of the type. If you assign a reference type to a variable, the variable applies the original value and does not create any replicas. The Go language reference types include slices, dictionaries, and channels.
Slices are typically used to implement variable-length arrays, and the prototype is defined as follows:
struct Slice
{
BYTE *array;
Unit32 Len;
Unit32 cap;
};
It is abstracted into the following three parts:
A pointer to the referenced underlying array, the number of elements in the slice, and the storage space allocated by the slice.
Ii. creation and declaration of slices
1. Creating slices based on the underlying array
var array1=[10]int{1,2,3,4,5,6,7,8,9,10}
var slice[]int
Slice1=array1[:5]
Slice2=array1[5:]
slice3=array1[:]
Slice4=array1
Slice5=array1[0:len (Array1)]
Add your own package and import and Func main to run the above code to see the effect, to understand the creation of slices based on the underlying array.
2. Create slices directly
var slice = []int{1,2,3,4,5}
3. Create a slice using the Make function
var slice=make ([]int,5)//Create an integer slice with 5 elements slice, the initial value of the element is 0.
When you create a slice using the make () function, you can also reserve storage space for the slice element:
var slice=make ([]int,5,10)//Create an Integer slice slice with 5 elements, the initial value of the element is 0, and the storage space of 10 elements is reserved.
Iii. access and traversal of slice elements
The access of the slices, as well as the array elements, is accessed through the following table of elements, or by using range to traverse all the slice elements. Example:
var slice=[]int{1,2,3,4,5}
For i:=0;i<=4;i++{
Fmt. Println (I,slice[i])
}
For I,v:=range slice{
Fmt. Println (I,slice[i])
}
Iv. operation of slices
1, the increase of the slice element
Use the Append () function to add a new element to the end of the slice:
:= make ([]string3) fmt< Span class= "P". println ( "EMP:" s ) /span>
s[] = "a" s[1< Span class= "P" >] = "B" s[2] = "C"
= append (s " D ")
< Span class= "NX" >s = append (s Span class= "s" > "E" "F" ) fmt.< Span class= "NX" >println ( "APD:" s )
2, slice copy:
var slice1=[]int{1,2,3,4,5,6,7,8,9,10}
var slice2=make ([]int,3,5)
var n int
N=copy (Slice2,slice1)
Fmt. Println (N,slice2,len (SLICE2), Cap (SLICE2))
Slice3:=slice[3:6]
N=copy (Slice3,slice1[1:5])
FMT. Println (N,SLICE,SLICE3)
This code requires a good study of the reader, which can deepen the slice reference is the underlying array of understanding, the result of the operation is:
3 [1 2 3] 3 5
3[1 2 3 2 3 4 7 8 9 10] [2 3 4]
See: