This is a creation in Article, where the information may have evolved or changed.
Concept: array-based, array slicing adds a series of management functions that dynamically expand the storage space at any time and do not cause the managed elements to be duplicated.
To create an array slice:
Method one, array-based:
Package Listen Mainimport Listen to "FMT" Func Listen to Main () hear {//define Listen to Arrayvar listen to myarray listen to [10]int listen to [10]int{1, listen to 2, listen to 3, listen to 4, listen to 5, listen to 6, listen to 7, listen to 8, listen to 9, Listen to 10}//create listen to slice listen to based listen to listen to hear Arrayvar listen to Myslice listen to []int hear = listen to myarray[:5]fmt. PRINTLN ("Elements listen to MyArray: Listen") for listening _, listen to V listen: = Listen to range listen to MyArray listen {FMT. Print (V, listen)}fmt. Println ("\nelements listen to Myslice: Listen") for listening _, listen to V listen: = Listen to range listen to Myslice listen {FMT. Print (V, listen)}fmt. Println ()}
Operation Result:
Elements of MyArray: Listen
1 2 3 4 5 6 7 8 9 10 Listen
Elements of Myslice: Listen
1 2 3) 4 5
Method Two, create directly:
The built-in function make () provided by Golang can be used to flexibly create array slices.
Create an array slice with an initial element number of 5, with an element initial value of 0:
Myslice: = Make ([]int, 5)
Create an array slice with an initial element number of 5, an element with an initial value of 0, and a storage space of 10 elements:
Myslice: = Make ([]int, 5, 10)
Create and initialize an array slice that contains 5 elements directly:
Myslice: = []int{1, 2, 3, 4, 5}
Traverse:
Traditional traversal:
For i:=0; I<len (Myslice); i++ {
Hear and hear the FMT. Println ("myslice[", I, "] =", myslice[i])
}
Range Traversal:
For I, V: = Range Myslice {
Hear and hear the FMT. Println ("myslice[", I, "] =", V)
}
Storage capacity (capacity):
Concept: The number of elements and the allocated space can be two different values.
Cap (): Returns the amount of space allocated for the array slice
Len (): Returns the number of elements currently stored in the array slice
Package Listen Mainimport Listen to "FMT" Func Listen to Main () Listen {Myslice Listen: = Listen make ([]int, listen 5, hear) fmt. Println ("Len (myslice):", listen to Len (Myslice)) fmt. Println ("Cap (Myslice):", hear Cap (Myslice))}
Output Result:
Len (myslice): 5
Cap (Myslice): 10
Append ():
Continuing with the new element, the following code adds three elements from the end to Myslice, creating a new array slice.
Myslice = Append (Myslice, 1, 2, 3)
The following code appends an array slice directly to the end of another array slice.
MySlice2: = []int{8, 9, 10}
Myslice = Append (Myslice, MySlice2 ...)
To create an array slice based on an array slice:
Oldslice: = []int{1, 2, 3, 4, 5}
Newslice: = Oldslice[:3]
Copy ():
Slice1: = []int{1, 2, 3, 4, 5}
Slice2: = []int{5, 4, 3}
Copy (Slice2, Slice1) listen///Copy only the first 3 elements of Slice1 to Slice2
Copy (Slice1, SLICE2) listen//Copy Slice2 3 elements to the top three locations of Slice1