当前位置:网站首页>Slice of go language foundation

Slice of go language foundation

2022-06-11 09:36:00 Ink purple feather ink

One 、 section :

1、 section : A slice is a reference to an array , So slices are reference types . But it's a structure in itself , Value copy passing . 2、 The length of the slice can be changed , therefore , Slicing is a variable array . 3、 Slice traversal is the same as array traversal , It can be used len() Find the length . Represents the number of available elements , Read and write operations cannot exceed this limit . 4、cap We can work out slice Maximum expansion capacity , Cannot exceed array limit .0 <= len(slice) <= len(array), among array yes slice Array of references . 5、 Definition of slice :var Variable name [] type , such as var str []string var arr []int. 6、 If slice == nil, that len、cap The result is equal to 0.

1、 Defining slices

package main

import "fmt"

func test01() {
	//1. Declaration slice 
	var s1 []int
	fmt.Println("s1:", s1)
	if s1 == nil {
		fmt.Println("s1 It's empty ")
	} else {
		fmt.Println("s1 Not empty ")
	}

	// 2.:=
	s2 := []int{}
	fmt.Println("s2:", s2)

	// 3.make()
	var s3 []int = make([]int, 5)
	fmt.Println("s3:", s3)

	// 4. Initialize assignment 
	var s4 []int = make([]int, 0, 0)
	fmt.Println("s4:", s4)
	s5 := []int{1, 2, 3}
	fmt.Println("s5:", s5)
	
	// 5. Slice from array 
	arr := [5]int{1, 2, 3, 4, 5}
	var s6 []int
	//  Front bag, back bag 
	s6 = arr[1:4]
	fmt.Println("s6:", s6)
}

func main() {
	test01()
}

Running results :

2、 Slice initialization

func test02() {
	arr := [...]int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
	slice1 := arr[2:8]
	slice2 := arr[:6]
	slice3 := arr[5:]
	slice4 := arr[:]
	slice5 := arr[:len(arr)-1] // Remove the last element of the slice 
	fmt.Printf("arr %v\n", arr)
	fmt.Printf("slice1: %v\n", slice1)
	fmt.Printf("slice2: %v\n", slice2)
	fmt.Printf("slice3: %v\n", slice3)
	fmt.Printf("slice4: %v\n", slice4)
	fmt.Printf("slice5: %v\n", slice5)
}

Running results :

3、 Modification of slices :

func test03() {
	darr := [...]int{57, 89, 90, 82, 100, 78, 67, 69, 59}
	dslice := darr[2:5]
	fmt.Println("array before", darr)
	for i := range dslice {
		dslice[i]++
	}
	fmt.Println("array after", darr)
}

Running results :

4、append Method :

func test04() {
	slice1 := []int{1, 2, 3}
	fmt.Println("slice1:", slice1)
	slice2 := append(slice1, 4, 5, 6)
	fmt.Println("slice2:", slice2)
	slice3 := []int{7, 8, 9}
	slice4 := append(slice1, slice3...)
	fmt.Println("slice4:", slice4)
}

Running results :

5、len() and cap()

func test05() {
	slice1 := []string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
	slice2 := slice1[1:3]
	fmt.Printf("len: %d cap: %d", len(slice2), cap(slice2))
}

Running results :

6、copy()

func test06() {
	s1 := []int{1, 2, 3, 4, 5}
	s2 := make([]int, 10)
	fmt.Printf("before s1 : %v\ns2 : %v\n", s1, s2)
	copy(s2, s1)
	fmt.Printf("after s1 : %v\ns2 : %v\n", s1, s2)
}

Running results :

7、 Traverse

func test07() {
	slice1 := []string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"}
	for id, v := range slice1 {
		fmt.Printf("id:%v,	value:%v\n", id, v)
	}
}

Running results :

原网站

版权声明
本文为[Ink purple feather ink]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012249193441.html