Go language slices (Slice)
The Go language slice is an abstraction of an array.
The length of the go array is immutable, and in a particular scenario such a collection is not very suitable, go provides a flexible, powerful built-in type slice ("dynamic array"), compared to the array length is not fixed, you can append elements, may increase the capacity of the slice when appended.
Defining slices
You can declare an array of unspecified size to define a slice:
var identifier []type
The slice does not require a description length.
or use the Make () function to create a slice:
var slice1 = make ([] type, len)
or write as follows
Slice1: = make ([] type, len)
You can also specify the capacity, where capacity is an optional parameter
Slice1: = make ([] type, Len, capacity)
Examples of slices
1 Package Main2 3 Import (4 "FMT"5 )6 7 Func Main () {8 //Initializes an array of blance, with an element of 1,2,3,4,5,69 varBlance = []int{1,2,3,4,5,6}Ten //Initialize array Slice1 because it is not given a value, it is populated by default by 3 0 One varSlice1 = make ([]int,3,5) A - //The initialized value of the output array -Fmt. Println ("Initialize the value of the array blance:", Blance) theFmt. Println ("Initialize the value of the array Slice1:", Slice1) - - //the operation of the slice, which can be evaluated by the index -Fmt. Println ("the value of the second index in the blance array is:", blance[2]) + - //modifies the last value in a blance array +blance[5] =10000 AFmt. Println ("Modify the result of the last value of the blance array:", Blance) at - //use a slice to assign a value to an S array, from the No. 0 index in the blance array to the third index, but not the value of the third index -S: = blance[:3] -Fmt. Println ("the blance array slice gets the value of the array s:", s) - -}
The result of the above code execution:
Initialize the value of the array blance: [1 2 3 4 5 6 " initializes the value of the array Slice1: [ 0 0 0 The value of the second index in the blance array is: 3 Result of modifying the last value of the blance array: [ 1 2 3 4 5 10000 ]blance array slice gets the value of the array s: [ 1 2 3 ]
Len () and Cap () functions
Slices are indexable and can be obtained by the Len () method.
Slicing provides a method for calculating capacity the CAP () can measure how long the slice can reach
1 Package Main2 3 Import (4 "FMT"5 )6 7 //Len can get the total length of the array, and the cap can get the maximum length of the slice8Func printslice (x []int, namestring ){9Fmt. Printf ("len=%d cap=%d%v=%v \ n", Len (x), Cap (x), name, X)TenFmt. Println ("--------------------------------") One } A - Func Main () { - //Initializes an array of blance, with an element of 1,2,3,4,5,6 the varBlance = []int{1,2,3,4,5,6} - - //call a function that prints a slice -Printslice (Blance,"blance") + - //Initializes an array with a maximum length of 8, initializes an array of 3 0 s +S: = make ([]int,3,8) AFmt. Println ("the value of the initialized array s is:", s) at - //call a function that prints a slice -Printslice (s),"s") - -}
The result of the above code execution:
len=6 cap=6 blance=[123456----- the value of the---------------------------initialized array s is: [000]len=3 cap=8 s=[000--------------------------------
Append () and copy () functions
If you want to increase the capacity of a slice, we must create a new larger slice and copy the contents of the original Shard.
The following code describes the copy method from the copy slice and the Append method that appends the new element to the slice.
1 Package Main2 3 Import (4 "FMT"5 )6 7 //Len can get the total length of the array, and the cap can get the maximum length of the slice8Func printslice (x []int, namestring ){9Fmt. Printf ("len=%d cap=%d%v=%v \ n", Len (x), Cap (x), name, X)TenFmt. Println ("--------------------------------") One } A - Func Main () { - varNumbers []int thePrintslice (Numbers,"numbers") - - //Append empty slices -Numbers =Append (numbers,) +Printslice (Numbers,"numbers") - + //append a single element ANumbers = append (Numbers,1) atPrintslice (Numbers,"numbers") - - //append multiple elements at the same time -Numbers = append (Numbers,9,9,Bayi) -Printslice (Numbers,"numbers") - in //creating a N1 array is twice times the capacity of the numbers -N1: = Make ([]int, Len (Numbers), Len (numbers) *2) toPrintslice (N1,"N1") + - //copies the contents of the numbers array into the N1, overwriting the existing content where the corresponding index is the copy (n1, numbers) *Printslice (N1,"N1") $ Panax Notoginseng //declare local data test, initialize element two 2, array allows maximum of 3 elements -Test: = Make ([]int,2,3) thePrintslice (Test,"Test") + //To add data to the test array, because more than 3 of the maximum length is exceeded, go will maintain memory and will request a value multiplied by the maximum capacity of * * To assign to the test array ATest = append (Test,1,2,3) thePrintslice (Test,"Test") +}
The result of the above code execution:
len=0cap=0numbers=[] --------------------------------Len=0cap=0numbers=[] --------------------------------Len=1cap=1numbers=[1] --------------------------------Len=4cap=4numbers=[1 9 9 Bayi] --------------------------------Len=4cap=8n1=[0 0 0 0] --------------------------------Len=4cap=8n1=[1 9 9 Bayi] --------------------------------Len=2 cap=3 test=[0 0] --------------------------------Len=5 cap=6 test=[0 0 1 2 3] --------------------------------
Slices of the Go language