Go language -- detailed explanation of slice

Keywords: Programming Go network

Go language -- detailed explanation of slice

Blog description

The information involved in this article comes from Internet collation and personal summary, which means personal learning and experience summary. If there is any infringement, please contact me to delete, thank you!

Explain

Go language slicing is an abstraction of arrays.

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

Defining slices

Note: the length of the slice does not need to be specified

1. Declare an array of unspecified size to define slices

var identifier []type
//for example
var slice []int

2. Use the make() function to create slices

var slice1 []type = make([]type, len)
//It can also be abbreviated as
slice1 := make([]type, len)
//for example
slice := make([]type, len)

Slices are indexable

len() and cap() functions

The length can be obtained by the len() method.

Slicing provides a way to calculate capacity cap() can measure the maximum length of slicing

package main

import "fmt"

func main(){
	var number = make([]int,3,5)
	fmt.Printf("len=%d cap=%d 		slice=%v\n",len(number),cap(number),number)
}

//len=3 cap=5 slice=[0 0 0]
Nil slice

A slice is nil by default and 0 in length before it is initialized

package main

import "fmt"

func main(){
	var number []int
	fmt.Printf("len=%d cap=%d slice=%v\n",len(number),cap(number),number)
}

//len=0 cap=0 slice=[]

Slicing

You can set the lower bound and upper bound to cut slices [lower bound: upper bound]

package main

import "fmt"

func main() {
	//Create slice
	number := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}
	printSlice(number)

	//Print original slice
	fmt.Println("number == ", number)

	//Print sub slice from index 1 to index 4
	fmt.Println("number == ", number[1:4])

	//Print sub slice default lower limit
	fmt.Println("number == ", number[:3])

	//Default upper limit of print subsegment
	fmt.Println("number == ", number[4:])

}

func printSlice(x []int) {
	fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}

Result

Increase slice capacity

Principle: create a new and larger slice and copy the contents of the original slice

append() and copy() functions
package main

import (
	"fmt"
)

func main() {
	//Create slice
	var number []int
	printSlice(number)

	//Allow appending empty slices
	number = append(number,0)
	printSlice(number)

	//Add an element to the slice
	number = append(number,1)
	printSlice(number)

	//Add multiple elements at the same time
	number = append(number,2,3,4)
	printSlice(number)

	//New slices created are twice the capacity of previous slices
	number1 := make([]int,len(number),(cap(number))*2)

	//Copy number to number1
	copy(number1,number)
	printSlice(number1)
}

func printSlice(x []int) {
	fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}

Thank

Rookie tutorial

Omnipotent network

And the industrious self

Posted by adamjones on Wed, 29 Apr 2020 09:14:34 -0700