golang Manual of Slicing of Slice principle

  • 2020-06-12 09:17:58
  • OfStack

Slicing, a new concept introduced in the go language. It has some characteristics as follows:

Abstract arrays The array is not fixed in length Append element Slice capacity can be increased The size of the volume increases in slices

Let's put the above concepts in here, but the actual problem is to be solved.

Define or declare slices

First let's look at the statement slice:


var sliceName []type

After the definition is completed, we need to define the slice:


sliceName = make([]type, len)

It can also be abbreviated as:


sliceName := make([]type, len)

In the above example, we declared a slice, so let's run it now to see the results.


package main
import "fmt"
func main() {
 sliceName := make([]string, 3)
 fmt.Printf(" slice slice_name The length of the: len=%d \n", len(sliceName))
}

 // The operation results are as follows: 
 /*
 *  slice slice_name The length of the: len=3 
 */

So if you look at this and see if you've found a slice, which is our normal array of 1, what does it call a slice?

We can see another make function: make([]T, length, capacity)

capacity above is the capacity of the array, and length is the length of the array. When the length of the newly inserted element exceeds the capacity, one capacity will be automatically added to fill the data, but the slice space is an integer multiple of capacity. demo is as follows:


package main
import "fmt"
func main() {
 sliceName := make([]string, 3, 15)
 fmt.Printf(" slice slice_name The length of the: len=%d cap=%d \n", len(sliceName), cap(sliceName))
 sliceName[0] = " cheng "
 fmt.Println(sliceName)
 // The operation results are as follows: 
 /*
 *  slice slice_name The length of the: len=3 cap=15 
 * [ cheng  ]
 */
 
}

Initialize slice

Any variable or constant needs to be initialized before it can be used, but constants write the declaration and initialization directly at 1. Let's look at the initialization of slices:


// Initializing array 
 arr := [] int{1, 2, 3}
 fmt.Println(arr)
 // Initialize slice  sliceName  is   An array of arr  A reference to the 
 sliceName := arr[:]
 fmt.Println(sliceName)

 // Of course, the standard way to write a slice-referenced array is:  s := arr[startIndex:endIndex]
 // Among them startIndex and endIndex I can omit either of them. 
 // absent startIndex It's an array with index zero 0 . endIndex And the omission is the subscript  len-1

 // The operation results are as follows: 
 //[1 2 3]
 //[1 2 3]
  
 sliceName1 := arr[1:3]
 fmt.Println(sliceName1)
 // Intercept the slicing element subscript from  1 Start to 3 But it doesn't include 3  And copy to slice sliceName1
 // Operation results: [2 3]

Of course, the slice will also have the empty object nil, which will be generated if you do not initialize it after you declare it.

Section of the add, delete, change check

What happens when we create a slice, and we need to add elements to the slice? We can see the built-in append function: func append(slice []Type, elems... Type) []Type, code as follows:


// First we need to add the original slice and the element to be added as parameters append Function, 
  // and append Returns the 1 So the code is as follows: 
 sliceName = append(sliceName, 4)
 fmt.Println(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 instructions above indicate that   Our target slice accepts the source array and returns the number of elements copied. 

Of course, the go language does not provide the built-in remove function, but we can implement it with the append function as follows:


// To remove a position The elements of the 
// The main idea is to combine the data before and after the location into 1 Gets and assigns values to the original array 
sliceName = append(sliceName[:position],sliceName[position+1:]...)

conclusion

Slices are just arrays The array size of slices is variable The increase in the volume of slices is an integer multiple Any object can be empty nil Use of built-in functions append and copy

Related articles: