This is the 9th day of my participation in the August Wen Challenge.More challenges in August
What is slicing
A Golang slice is an abstraction of an array. Go provides a flexible and powerful built-in type slice (” dynamic array “). Compared with the array, the length of the slice is not fixed. You can append elements, which may increase the capacity of the slice
Slicing is a convenient, flexible and powerful wrapper. The slice itself does not have any data. They are just references to existing arrays
Compared with array, slice does not need to set the length and does not need to set the value in [], which is relatively free
Conceptually, slice is like a structure that contains three elements
- Pointer – Points to the start of slice in the array
- Length – the length of slice
- Maximum length – from the start of the slice to the end of the array to the length
Overview section
- A data structure that facilitates the use and management of our data sets
- Automatic growth on demand, dynamic array (done via Append)
- The bottom point is an array
- Memory is continuous storage space, which can effectively improve CPU execution efficiency
- Reference types
Slice of
- Pointer to the underlying array
- Slice the length of the element through
len()
In order to get - Capacity, obtained by cap()
Functions and application scenarios of slicing
Function: When passing slices in functions, when the data type data is large, using slices can effectively reduce memory consumption and improve program execution efficiency
Application scenario: Map slices are used to read commodity information from database tables. Each map is regarded as a whole
How to use slices
Define an array and let slices reference it
arr := [...] Int {1, 2, 3, 4, 5} var sliceArr []int sliceArr = arrCopy the code
Through the above code, analyze the underlying situation of the slice respectively print the address of the array and the slice address to analyze
- Definition of slice
Var arr []int // Define an array arr := [... int{1, 2, 3, 4, 5} for i:= 0; i < len(arr); I++ {FMT. Println (" arr = % d % d, address = % p \ n ", I, arr [I], & arr [I])}Copy the code
- Slice to reference an array
arr := [...] int{1, 2, 3, 4, 5} var sliceArr = arr[:] for i := 0; i < len(sliceArr); SliceArr i++ {FMT. Println (" % d = % d, address = % p \ n ", I, sliceArr [I], & sliceArr [I])}Copy the code
var arr1 = [...] Int {1, 2, 3, 4, 5} arr := arr1[1:3] fmt.Println(arr) // 1, 2, 3, 4, 5 // result: arr = [2, 3] i < len(arr); Arr i++ {FMT. Printf (" % d = % d, address = % p \ n ", I, arr [I]. &arr[i]) } arr[0] = 100 fmt.Println(arr1) // [1 2 3 4 5] fmt.Println(arr) // [100 3]Copy the code
Create slices by making
Var slice1 []type = make([]type, len, Capacity) // slice1 := make([]type, len, capacity) make([]T, length, // demo var arr []int = make([]int, 5, 6) FMT.Println("arr length =%d, capacity =%d, \n section points to the address of the bottom array =%p, Slice your own address =%p\n", len(arr), cap(arr), arr, &arr)Copy the code
The last
Just learning Golang, we hope everyone can supervise and work together
If you are interested, please follow me at EntrepreneurialG