# Go 学习笔记（四）- 复合数据类型

`var a [3]int // 数组声明var b [3]int = [3]int{1, 2, 3} // 数组声明+初始化fmt.Printf("%v, %v, %#v/n", len(a), a[0], a)fmt.Printf("%v, %v, %#v/n", len(b), b[0], b)// 3, 0, [3]int{0, 0, 0}// 3, 1, [3]int{1, 2, 3}`

`a := [...]int{1, 2, 3} // 简化声明b := [...]int{2: -1}fmt.Printf("%v, %v, %#v/n", len(a), a[0], a)fmt.Printf("%v, %v, %#v/n", len(b), b[2], b)// 3, 1, [3]int{1, 2, 3}// 3, -1, [3]int{0, 0, -1}`

## Slice（切片）

Slice（切片）代表变长的序列，slice 的语法和数组很像，只是没有固定长度而已。

`arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}a := arr[:]b := arr[:5]c := arr[5:]fmt.Printf("%v, %v, %#v/n", len(arr), cap(arr), arr)fmt.Printf("%v, %v, %#v/n", len(a), cap(a), a)fmt.Printf("%v, %v, %#v/n", len(b), cap(b), b)fmt.Printf("%v, %v, %#v/n", len(c), cap(c), c)// 10, 10, [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}// 10, 10, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}// 5, 10, []int{0, 1, 2, 3, 4}// 5, 5, []int{5, 6, 7, 8, 9}`

`arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}`

slice 之间不能最 `==` 比较，唯一合法的比较操作是和nil比较。

`arr := []int{1, 2, 3}arr = append(arr, 4, 5)b := []int{6, 7, 8}arr = append(arr, b...)fmt.Printf("%v, %v, %#v/n", len(arr), cap(arr), arr)// 8, 12, []int{1, 2, 3, 4, 5, 6, 7, 8}`

## Map

`a := make(map[string]int)a["aa"] = 11a["bb"] = 22b := map[string]int{  "aa": 11,  "bb": 22,}delete(b, "aa")fmt.Printf("%#v/n", a)fmt.Printf("%#v/n", b)// map[string]int{"aa":11, "bb":22}// map[string]int{"bb":22}`

`a := map[string]map[string]int{  "aa": {    "aaa": 111,  },  "bb": {    "bbb": 222,  },}fmt.Printf("%v/n", a)// map[aa:map[aaa:111] bb:map[bbb:222]]`

## 结构体

`type Point struct {  X int  Y int}// 或者type Point struct {  X, Y int}`

`a := Point{11, 22} // 按值的顺序b := Point{Y: 11, X: 22} // 通过成员赋值fmt.Printf("%+v/n", a)fmt.Printf("%+v/n", b)// {X:11 Y:22}// {X:22 Y:11}`

`type Point struct {  X, Y int}type Circle struct {  Center Point  Radius int}type Wheel struct {  Circle Circle  Spokes int}`

`a := Wheel{Circle{Point{11, 22}, 33}, 44}fmt.Printf("%+v/n", a)// {Circle:{Center:{X:11 Y:22} Radius:33} Spokes:44}`

## JSON

`type Movie struct {  Title  string  Year   int  `json:"released"`  Color  bool `json:"color,omitempty"`  Actors []string}`

`var movies = []Movie{  {Title: "Casablanca", Year: 1942, Color: false,    Actors: []string{"Humphrey Bogart", "Ingrid Bergman"}},  {Title: "Cool Hand Luke", Year: 1967, Color: true,    Actors: []string{"Paul Newman"}},  {Title: "Bullitt", Year: 1968, Color: true,    Actors: []string{"Steve McQueen", "Jacqueline Bisset"}},}data, err := json.Marshal(movies)if err != nil {  log.Fatalf("JSON marshaling failed: %s", err)}fmt.Printf("%s/n", data)// [{"Title":"Casablanca","released":1942,"Actors":["Humphrey Bogart","Ingrid Bergman"]},{"Title":"Cool Hand Luke","released":1967,"color":true,"Actors":["Paul Newman"]},{"Title":"Bullitt","released":1968,"color":true,"Actors":["Steve McQueen","Jacqueline Bisset"]}]`

json 数据里 Year 变成了 released，Color 变成了 color，而且 Color 为 false 时，json 数据里忽略了这个字段。

## 文本和HTML模板

`const templ = `{{.TotalCount}} issues:{{range .Items}}=======================Number: {{.Number}}User:   {{.User.Login}}Title:  {{.Title | printf "%.64s"}}Age:    {{.CreatedAt | daysAgo}} days{{end}}``