Slicing, this is a new concept introduced in the Go language. It has some characteristics as follows:
- Abstraction of arrays
- The array length is not fixed
- Additional elements
- Slice capacity can be increased
- Increase in capacity size
Let’s organize the above concept here first, but in fact, we still need to use codes to solve the problem.
Define or declare slices
First let's take a look at the statement slice:
var sliceName []type
After the definition is complete, we need to define the slice:
sliceName = make([]type, len)
It can also be appropriately abbreviated:
sliceName := make([]type, len)
In the example above, we declare a slice, and we now run it to see the results.
package main import "fmt" func main() { sliceName := make([]string, 3) ("sliceslice_nameLength:len=%d \n", len(sliceName)) } //The operation results are as follows: /* * Length of slice slice_name: len=3 */
So when you see this, do you find a slice, which is an ordinary array of ours? So why is it called a slice?
Can I see another make function: make([]T, length, capacity)
The capacity above is the capacity of the array, and the length is the length of the array. When the length exceeds the capacity after the new element is inserted, a capacity will be automatically added to fill the data, but the space size of the slice is an integer multiple of capacity. The demo is as follows:
package main import "fmt" func main() { sliceName := make([]string, 3, 15) ("sliceslice_nameLength:len=%d cap=%d \n", len(sliceName), cap(sliceName)) sliceName[0] = "Mr. Cheng" (sliceName) //The operation results are as follows: /* * Length of slice_name: len=3 cap=15 * [Mr. Cheng] */ }
Initialize slices
Any variable or constant needs to be initialized before use, but constants directly write the declaration and initialization together. Let's take a look at the initialization of the slice:
//Initialize the array arr := [] int{1, 2, 3} (arr) //Initialize slice sliceName is a reference to array arr sliceName := arr[:] (sliceName) //Of course, the standard way to write a slice reference array is: s := arr[startIndex:endIndex] //The startIndex and endIndex can be omitted. //The default startIndex is the array subscript is 0, and the endIndex omits the subscript is len-1 //The operation results are as follows: //[1 2 3] //[1 2 3] sliceName1 := arr[1:3] (sliceName1) //Seave the slice element subscript from 1 to 3 but does not include 3 and copy it to slice sliceName1 //Operation results: [2 3]
Of course, there will be empty nil in slices, and nil will be generated if you do not initialize after you declare.
Add, delete, modify and check slices
What should we do when we create a slice, we need to add the elements of the slice? We can see the built-in append function: func append(slice []Type, elems ...Type) []Type, the code is as follows:
//First of all, we need to add the original slice and the element to the append function as parameters. //And append will return a new slice, so the code is as follows: sliceName = append(sliceName, 4) (sliceName) //The operation results are as follows: // [1 2 3 4]
Of course, let's take a look at the copy function as follows:
// The copy built-in function copies elements from a source slice into a destination slice. // Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst). func copy(dst, src []Type) int // The above description indicates that our target slice accepts the source array and returns the number of elements to be copied.
Of course, the go language does not provide a built-in remove function, but we can implement it through the append function, as follows:
//Remove the element of a position//The main idea is to combine the previous data and the subsequent data at that location and assign it to the original arraysliceName = append(sliceName[:position],sliceName[position+1:]...)
Summarize
- Slices are actually arrays
- The size of the sliced array can be varied
- The capacity increase of slices is an integer multiple
- Any object may be empty nil
- Use of built-in functions append and copy
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.