The third chapter, Go- built-in container

3.1. Array

(1) Definition of the array

main Package 

Import ( 
	"FMT" 
) 

FUNC main () { 
	// define an array can not use var initial value 
	var of arr1 [. 5] // int [0 0 0 0 0] 
	// initial value must be specified by colons 
	arr2: = [. 3] {l, 3,5} // int [. 1. 3. 5] 
	// with "..." denotes any number 
	arr3: = [...] int { 2,4,6,8,10} // [2. 8. 6 10. 4] 
	fmt.Println (of arr1, arr2 is, ARR3) 
}

Traversing (2) of the array

With the range keyword

main Package 

Import ( 
	"FMT" 
) 

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

	// array traversal 
	for I: = {Range ARR3 
		fmt.Println (ARR3 [I]) 
	} 
} 


// results 
2 
. 4 
. 6 
. 8 
10

Acquiring index and value

main Package 

Import ( 
	"FMT" 
) 

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

	// array traversal 
	for I, V: = Range ARR3 { 
		fmt.Println (I, V) 
	} 
} 

// results 
0 2 
. 1. 4 
2. 6 
. 3. 8 
. 4 10

(3) is a value type array

  • [10] int and [20] int different types
  • Call func f (arr [10] int) will be copied array
  • Generally not used in an array of languages ​​go, while using slices
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)
}


//结果
0 2
1 4
2 6
3 8
4 10

 If the error pass arr2

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. Slice

(1) sliced

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 backward, not forward extension

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 s2 is accessible to [5,6] of

 

 Implement (3) slice of

 slice the bottom three hidden value

s [i] may not exceed len (s), it may not extend rearwardly beyond 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.切片的操作

(1)向slice添加元素

  • 添加元素时,如果超出了cap,系统会重新分配更大的底层数组
  • 由于值传递的关系,必须接收append的返回值。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]
	//因为s2的cap是[5,6,7],s3 append把7替换成10,s4后超出了cap
	fmt.Println(arr)         //[0 1 2 3 4 5 6 10]
}

(2)slice的创建

package main

import "fmt"

func main() {
	//第一种
	//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]

	//第二种
	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

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

	//第四种
	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)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]
	//删除index为3的值
	s1 = append(s1[:3],s1[4:]...)
	fmt.Println(s1)                   //[0 1 2 4 5 6]
}

  

Guess you like

Origin www.cnblogs.com/derek1184405959/p/11285729.html