# Go - 数组

## 概述

len()cap() 返回结果始终一样。

## 声明数组

package main

import (
"fmt"
)

func main() {
//一维数组
var arr_1 [5] int
fmt.Println(arr_1)

var arr_2 =  [5] int {1, 2, 3, 4, 5}
fmt.Println(arr_2)

arr_3 := [5] int {1, 2, 3, 4, 5}
fmt.Println(arr_3)

arr_4 := [...] int {1, 2, 3, 4, 5, 6}
fmt.Println(arr_4)

arr_5 := [5] int {0:3, 1:5, 4:6}
fmt.Println(arr_5)

//二维数组
var arr_6 = [3][5] int {{1, 2, 3, 4, 5}, {9, 8, 7, 6, 5}, {3, 4, 5, 6, 7}}
fmt.Println(arr_6)

arr_7 :=  [3][5] int {{1, 2, 3, 4, 5}, {9, 8, 7, 6, 5}, {3, 4, 5, 6, 7}}
fmt.Println(arr_7)

arr_8 :=  [...][5] int {{1, 2, 3, 4, 5}, {9, 8, 7, 6, 5}, {0:3, 1:5, 4:6}}
fmt.Println(arr_8)
}

## 注意事项

var arr_1 = [5] int {1, 2, 3, 4, 5}
arr_1[5] = 6
fmt.Println(arr_1)

//demo_5.go
package main

import (
"fmt"
)

func main() {
var arr =  [5] int {1, 2, 3, 4, 5}
modifyArr(arr)
fmt.Println(arr)
}

func modifyArr(a [5] int) {
a[1] = 20
}

//demo_6.go
package main

import (
"fmt"
)

func main() {
var arr =  [5] int {1, 2, 3, 4, 5}
modifyArr(&arr)
fmt.Println(arr)
}

func modifyArr(a *[5] int) {
a[1] = 20
}

var arr =  [5] int {1, 2, 3, 4, 5}
var arr_1 [5] int = arr
var arr_2 [6] int = arr

## 推荐阅读

posted @ 2019-06-28 10:01  新亮笔记  阅读(...)  评论(...编辑  收藏