slice of Go language

Posted by systemick on Fri, 04 Feb 2022 08:29:39 +0100

What is slice

The length of the go array cannot be changed, so such a set is not applicable in a specific scene. Go provides a flexible and powerful built-in type slice ("dynamic array"). Compared with the array, the length of the slice is not fixed, and elements can be added, which may increase the capacity of the slice

The slice type of Go provides a convenient and efficient way to process the same type of data sequence. Slicing is somewhat similar to arrays in other languages, but has some unusual features. This article will Go deep into the essence of slicing and explain its usage.

Slicing principle

Define slice

// First kind
var identifier []type
//The second is to use make
var slice1 []type = make([]type, len)
//Abbreviated as
slice1 := make([]type, len)
//Use make to create slice, map and chanel. The instructions are as follows

//The third way is to return by operating on an array
course := [5]string{"django", "tornado", "scrapy", "python", "asyncio"}
subCourse := course[1:2]
fmt.Printf("%T", subCourse)

Slice initialization

s :=[] int {1,2,3 } 
Initialize the slice directly,[]Indicates the slice type,{1,2,3}The initialization values are 1 in turn,2,3.his cap=len=3
s := arr[:] 

Serial numbermodeCode example
1Direct declarationvar slice []int
2newslice := *new([]int)
3Literal slice := []int{1,2,3,4,5}
4makeslice := make([]int, 5, 10)
5"Intercept" from slice or arraySlice: = array [1:5] or slice: = sourceslice [1:5]

Append append

slice is a dynamic array, so we need to add values dynamically

package main

import (
	"fmt"
)

func main() {
	var course = [5]string{"django", "scrapy", "tornado", "flask", "docker"}
	slice1 := course[1:4]
	
	// Append can append elements to the slice
	slice1 = append(slice1, "go") 
	fmt.Println(slice1) //[scrapy tornado flask go]
	

}
	// Append append multiple elements
	slice1 = append(slice1, "go","python1", "python2") 
	fmt.Println(slice1) //[scrapy tornado flask go python1 python2]

Slice merge

	subCourse2 := course[1:3]
	appendedCourse := []string{" imooc"," imooc2" ," imooc3"}
	subCourse2 = append(subCourse2, appendedCourse...) //Parameter passing rules of function
	fmt.Println(subCourse2 )

Slice copy

package main

import (
	"fmt"
)

func main() {
	var course = [5]string{"django", "scrapy", "tornado", "flask", "docker"}
	slice1 := course[1:4]
	slice2 := make([]string, len(slice1))
	copy(slice2, slice1)
	fmt.Println(slice2) // [scrapy tornado flask go]
	
	// Note that when the length is 2, only the first two can be copied
	slice3 := make([]string, 2)
	copy(slice3, slice1)
	fmt.Println(slice3) //[scrapy tornado]
}

Slice deletion

	deleteCourse := [5]string{"django", "scrapy", "tornado", "flask", "docker"}
	courseSlice := deleteCourse[:] // Convert to slice
	
	// Delete second
	courseSlice = append(courseSlice[:1], deleteCourse[2:]...)
	fmt.Println(courseSlice) //[django tornado flask docker] 

slice capacity expansion mechanism

The parameter length of the append function is variable, so you can append multiple values to slice. You can also use Pass in slice and directly append a slice.
The return value of the append function is a new slice. The Go compiler does not allow you to call the append function without using the return value.
Use append to append elements to slice, which is actually adding elements to the underlying array. However, the length of the underlying array is fixed. If the element pointed to by the index len-1 is the last element of the underlying array, it cannot be added.
At this time, slice will migrate to a new memory location, and the length of the new underlying array will increase, so that the new elements can be placed. At the same time, in order to cope with the append operation that may occur again in the future, a certain buffer is reserved for the length of the new underlying array, that is, the capacity of the new slice. Otherwise, every time you add elements, migration will occur, and the cost is too high.

Topics: Go Back-end