Chapter Three, Go-Built-in Container

Chapter 3. Go-Built-in Container

3.1. Array

(1) The definition of the array

package main

import (
	"fmt"
)

func main() {
	//Use var to define an array without assigning initial value
	var arr1 [5]int//[0 0 0 0 0]
	//Use a colon to specify the initial value
	arr2 := [3]int{1,3,5}//[1 3 5]
	//Use "..." to indicate any number
	arr3 := [...]int{2,4,6,8,10}//[2 4 6 8 10]
	fmt.Println(arr1, arr2, arr3)
}

(2) Traversal of the array

Use the range keyword

package main

import (
	"fmt"
)

func main() {
	arr3 := [...]int{2,4,6,8,10}//[2 4 6 8 10]
	fmt.Println(arr3)

	//The traversal of the array
	for i := range arr3{
		fmt.Println(arr3[i])
	}
}


//result
2
4
6
8
10

Get subscript and value

package main

import (
	"fmt"
)

func main() {
	arr3 := [...]int{2,4,6,8,10}//[2 4 6 8 10]
	fmt.Println(arr3)

	//The traversal of the array
	for i ,v:= range arr3{
		fmt.Println(i,v)
	}
}

//result
0 2
1 4
2 6
3 8
4 10

(3) Array is a value type

  • [10]int and [20]int are different types
  • Calling func f(arr [10]int) will copy the array
  • In the go language, arrays are generally not used, but slices are used
package main

import (
	"fmt"
)

func printArray(arr [5]int){
	for i ,v:= range arr{
		fmt.Println(i,v)
	}
}

func main() {
	//arr2 := [3]int{1,3,5}
	arr3 := [...]int{2,4,6,8,10}    
	printArray(arr3)
}


//result
0 2
1 4
2 6
3 8
4 10

 Report an error if arr2 is passed

package main

import (
	"fmt"
)

func printArray(arr [5]int){
	for i ,v:= range arr{
		fmt.Println(i,v)
	}
}

func main() {
	arr2 := [3]int{1,3,5}
	//arr3 := [...]int{2,4,6,8,10}
	printArray(arr2)//cannot use arr2 (type [3]int) as type [5]int in argument to printArray
}

3.2. Slicing

(1) Slice

package main

import "fmt"

func main() {
	arr := [...]int{0,1,2,3,4,5,6,7}
	fmt.Println(arr[2:6])//[2 3 4 5]
	fmt.Println(arr[:6])//[0 1 2 3 4 5]
	fmt.Println(arr[2:])//[2 3 4 5 6 7]
	fmt.Println(arr[:])//[0 1 2 3 4 5 6 7]
}

(2) Slice extension

Slice can be extended backwards, but cannot be extended forwards

package main

import "fmt"

func main() {
	arr := [...]int{0,1,2,3,4,5,6,7}
	s1 := arr[2:6]
	s2 := s1[3:5]
	fmt.Println(s1)//[2 3 4 5]
	fmt.Println(s2)//[5 6]
}

 How did s2 get [5,6]

 (3) The realization of slice

 3.values ​​are hidden at the bottom of slice

s[i] cannot exceed len(s), and backward expansion cannot exceed the underlying array cap(s)

package main

import "fmt"

func main() {
	arr := [...]int{0,1,2,3,4,5,6,7}
	s1 := arr[2:6]
	s2 := s1[3:5]
	fmt.Println("arr = ",arr)//arr = [0 1 2 3 4 5 6 7]
	fmt.Printf("s1=%v,len(s1)=%d,cap(s1)=%d\n",s1,len(s1),cap(s1))//s1=[2 3 4 5 ],len(s1)=4,cap(s1)=6
	fmt.Printf("s2=%v,len(s2)=%d,cap(s2)=%d\n",s2,len(s2),cap(s2))//s2=[5 6], len(s2)=2,cap(s2)=3
	fmt.Println(s1[3:6])//[5 6 7]
}

3.3. Slice operation

(1) Add elements to slice

  • When adding elements, if the cap is exceeded, the system will reallocate a larger underlying array
  • Due to the value transfer, the return value of append must be received. s = append(s,val)
package main

import "fmt"

func main() {
	arr := [...]int{0,1,2,3,4,5,6,7}
	s1 := arr[2:6]//[2 3 4 5]
	s2 := s1[3:5]//[5 6]
	s3 := append(s2,10)
	s4 := append(s3,11)
	s5 := append(s4,12)
	fmt.Println(s3,s4,s5)//[5 6 10] [5 6 10 11] [5 6 10 11 12]
	//Because the cap of s2 is [5,6,7], s3 append replaces 7 with 10, and s4 exceeds the cap
	fmt.Println(arr)//[0 1 2 3 4 5 6 10]
}

(2) The creation of slice

package main

import "fmt"

func main() {
	//The first
	//Zero value for slice is nil
	var s1 []int
	fmt.Println(s1)//[]

	for i := 0; i <5; i++{
		s1 = append(s1,i)
	}
	fmt.Println(s1)//[0 1 2 3 4]

	//The second
	s2 := []int{2,4,6,8}
	fmt.Printf("len(s2)=%d,cap(s2)=%d\n",len(s2),cap(s2))//len(s2)=4,cap(s2)=4

	//The third
	s3 := make([]int,16)
	fmt.Printf("len(s3)=%d,cap(s3)=%d\n",len(s3),cap(s3))//len(s3)=16,cap(s3)=16

	//The fourth
	s4 := make([]int,16,32)
	fmt.Printf("len(s4)=%d,cap(s4)=%d\n",len(s4),cap(s4))//len(s4)=16,cap(s4)=32
}

(3) Deletion of slice

package main

import "fmt"

func main() {
	s1 := []int{0,1,2,3,4,5,6}
	fmt.Println(s1)//[0 1 2 3 4 5 6]
	//Delete the value of index 3
	s1 = append(s1[:3],s1[4:]...)
	fmt.Println(s1)//[0 1 2 4 5 6]
}
Reference: https://cloud.tencent.com/developer/article/1479620 Chapter 3, Go-Built-in Container-Cloud + Community-Tencent Cloud