Go: 3-1 数组、切片和容器

数组 (值类型)
   //数组初始化
    var arr1 [5]int
   	arr2 :=[3]int{2,4,6}
   	arr3 :=[...]int{2,4,5,6,78,0}
   
   	fmt.Println(arr1,arr2,arr3)
   
   	var grid[4][2][1]int
   	fmt.Println(grid)
   
   	//元素遍历
   	for i :=0;i < len(arr3) ;i ++  {
   		fmt.Println(arr3[i])
   	}
   	
   	
//指针传递   	
func printArray(arr *[5]int) {
	arr[0] = 100
	for i, v := range arr {
		fmt.Println(i, v)
	}
}


range 关键字 (还会有很多其他地方用到)
   	for i,v := range arr3  {
   		fmt.Println(i,v)
   	}
   	for _,v := range arr3  {
   		fmt.Println(v)
   	}
切片 Slice
    arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7}

	fmt.Println("arr[2:6] =", arr[2:6])
	fmt.Println("arr[:6] =", arr[:6])
	s1 := arr[2:]
	fmt.Println("s1 =", s1)
	s2 := arr[:]
	fmt.Println("s2 =", s2)

	fmt.Println("After updateSlice(s1)")
	updateSlice(s1)
	fmt.Println(s1)
	fmt.Println(arr)

	fmt.Println("After updateSlice(s2)")
	updateSlice(s2)
	fmt.Println(s2)
	fmt.Println(arr)

	fmt.Println("Reslice")
	fmt.Println(s2)
	s2 = s2[:5]
	fmt.Println(s2)
	s2 = s2[2:]
	fmt.Println(s2)

	fmt.Println("Extending slice")
	arr[0], arr[2] = 0, 2
	fmt.Println("arr =", arr)
	s1 = arr[2:6]
	fmt.Println(arr[0:1])
	s2 = s1[3:6] // [s1[3], s1[4]]
	fmt.Printf("s1=%v, len(s1)=%d, cap(s1)=%d\n",
		s1, len(s1), cap(s1))
		
		
	fmt.Printf("s2=%v, len(s2)=%d, cap(s2)=%d\n",
		s2, len(s2), cap(s2))

	s3 := append(s2, 10)
	s4 := append(s3, 11)
	s5 := append(s4, 12)
	fmt.Println("s3, s4, s5 =", s3, s4, s5)
	// s4 and s5 no longer view arr.
	fmt.Println("arr =", arr)

	// Uncomment to run sliceOps demo.
	// If we see undefined: sliceOps
	// please try go run slices.go sliceops.go
	fmt.Println("Uncomment to see sliceOps demo")
	// sliceOps()

Slice的向后扩展
    arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7}
    s1 = arr[2:6]
	s2 = s1[3:5]

向slice添加元素