1 数组

1.1 数组的声明

• 指定数组的长度，赋值为默认零值
``````var my_array [5]int
``````
• 指定数组的长度，赋值为指定的值
``````var my_array [5]int{1, 2, 3, 4, 5}
``````
• 不指定数组的长度，赋值为指定的值，数组长度由初始化时数组的元素个数来确定
``````var my_array [...]int{1, 2, 3, 4, 5}
``````
• 指定数组的长度，特定的位置赋值为特定的值
``````var my_array [5]int{1:10, 3:30}
``````

1.2 数组的使用

数组的赋值

``````var array1 [5]string

var array2 = [5]string{"Red", "Blue", "Green", "Yello", "Pink"}

array1 = array2
fmt.Printf("Array1 is %#v\n", array1)
fmt.Printf("Array2 is %#v\n", array2)

array1[1] = "Other"

fmt.Printf("Array1 is %#v\n", array1)
fmt.Printf("Array2 is %#v\n", array2)
``````

``````Array1 is [5]string{"Red", "Blue", "Green", "Yello", "Pink"}
Array2 is [5]string{"Red", "Blue", "Green", "Yello", "Pink"}
Array1 is [5]string{"Red", "Other", "Green", "Yello", "Pink"}
Array2 is [5]string{"Red", "Blue", "Green", "Yello", "Pink"}
``````

2 切片

• 地址指针
• 长度（能够访问的元素的个数）
• 容量（允许增加到的元素个数）

2.1 切片的创建

• 使用长度创建切片
``````slice := make([]string, 5)
``````
• 使用长度和容量创建切片
``````slice := make([]string, 3, 5)
``````
• 使用字面量来创建切片
``````slice := []string{"Red", "Blue", "Green", "Yellow", "Pink"}
``````
• 使用索引声明切片
``````slice := []string{99: ""}
``````

2.2 nil和空切片

• nil切片：
``````var slice []int
``````
• 空切片：
``````slice := make([]int, 0)

slice := []int{}
``````

2.4 切片增长

``````slice := []int{10, 20, 30, 40, 50}
newSlice := slice[1:3]
newSlice = append(newSlice, 60)
fmt.Printf("slice is %#v\n", slice)
fmt.Printf("newSlice is %#v\n", newSlice)
``````

``````slice is []int{10, 20, 30, 60, 50}
newSlice is []int{20, 30, 60}
``````

``````slice := []int{10, 20, 30, 40}
newSlice := slice[1:4]
newSlice = append(newSlice, 50)
fmt.Printf("slice is %#v\n", slice)
fmt.Printf("newSlice is %#v\n", newSlice)
``````

``````slice is []int{10, 20, 30, 40}
newSlice is []int{20, 30, 40, 50}
``````

2.5 三个索引

`slice[i:j:k]`

• i：开始的位置
• j：开始的位置+希望包括的元素的个数（和两个索引的时候一样）
• k：开始的位置+希望容量的个数
``````source := []string{"Apple", "Orange", "Plum", "Banana", "Grape"}
slice := source[2:3:4]
fmt.Printf("source is %#v\n", source)
fmt.Printf("slice is %#v\n", slice)
``````

``````source is []string{"Apple", "Orange", "Plum", "Banana", "Grape"}
slice is []string{"Plum"}
``````

``````source := []string{"Apple", "Orange", "Plum", "Banana", "Grape"}
slice := source[2:3:3]
slice = append(slice, "Kiwi")
fmt.Printf("source is %#v\n", source)
fmt.Printf("slice is %#v\n", slice)
``````

``````source is []string{"Apple", "Orange", "Plum", "Banana", "Grape"}
slice is []string{"Plum", "Kiwi"}
``````

• len：获得切片的长度
• cap：获得切片的容量

3 映射

3.1 映射的创建

• 使用make创建
``````dict := make(map[string]int)
``````
• 使用字面量创建
``````dict := map[string]string{
"Red"    :"#da1337",
"Orange" :"#e95a22"
}
``````

3.2 映射的使用

键是否存在

``````v, exists := colors["Blue"]
``````

删除

``````delete(<map>, <key>)
``````