Go array

Array

An array is a collection of elements of the same type. All elements should all be of the same type. For example, an array of integers 5,8,9,79,76 formed. Go languages ​​allowed mixing different types of elements, for example, an array containing strings and integers. (Of course, if the interface {} is an array type, can contain any type)

First, the definition of an array

1, declare an array

grammar:

var 数组名 [元素个数]元素类型  // 数组大小固定,不能多

An array of default values ​​and type of storage array related.

var lis [3] int
fmt.Println(lis)  // [0 0 0] int的默认值是0

2, the initialization set value

grammar:

var 数组名 [元素个数]元素类型 = [元素个数]元素类型{元素1,元素2,元素3}

定义每个元素
//var lis [3]int = [3]int{1, 2, 3}
// lis := [3]int{1, 2, 3}
var lis = [3]int{1, 2, 3}
fmt.Println(lis)   // [1 2 3]
fmt.Printf("%T",lis)  // [3]int

3, designated position setter

Just want to set a location of the element.

grammar:

[元素个数]元素类型{索引:值} ,其它都是默认值

var a [30]int=[30]int{28:1,23:999}

4, initialization is not specified length (Learn)

[]Not within the specified length, but [...]an array of length such statement is initialized maximum length. Instead of variable length arrays.

了解 (这不是可变长)
a :=[...]int{28:1,23:999}  //长度为29
a :=[...]int{2,3,4}  //长度为3
// a :=[3]int{2,3,4}  //长度为3

Second, the use of an array

Index values

var lis = [3]int{2, 4, 6}
fmt.Println(lis[0],lis[2])

2 6

Directly modify the value

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

[2 4 6]
[2 666 6]

Third, the type of the array

[元素个数]元素类型   // 合在一起才是一个数组的类型,数组的大小也是类型的一部分

An array is a value type, not a reference type (address).

go language function parameter passing, all copy delivery. Whether it is a value type or reference type. A copy will, pass into this function.

If the value is a variable of type, pass into functions after modification does not affect the copy variables. As it has been different variables.

But if it is a reference type variable passed into the function after the changes will affect other reference variables of this address. Because the value of this address directly modified.

Fourth, the length of the array

With built-in functions len()to obtain the length of the array.

var a =[30]int{2,4,6}
fmt.Println(len(a))  //3

Fifth, the array iteration

1, initialize iteration

var lis  = [3]int{2,4,6}
for i:=0;i<len(lis);i++{
    fmt.Println(lis[i])
}

2
4
6

2, using an iterative range

//for i:=range a{}   //只用一个值来接收,就是索引
//for i,v:=range a{}  //如果两个值来接收,就是索引(i)和值(v)
//for _,v:=range a{}  //只接受值

var lis  = [3]int{2,4,6}

for i,v:=range lis{
    fmt.Println(i,v)
}

0 2
1 4
2 6

Sixth, multidimensional arrays

var a [3][3]int
fmt.Println(a)
[[0 0 0] [0 0 0] [0 0 0]]
----------------------------------
var a [3][3]int=[3][3]int{{1,2,3},{4,5,6},{7,8}}
a[0][1]=999
fmt.Println(a)
[[1 999 3] [4 5 6] [7 8 0]]
----------------------------------
var a [3][3][3]int
fmt.Println(a)
[
    [[0 0 0] [0 0 0] [0 0 0]] 
    [[0 0 0] [0 0 0] [0 0 0]] 
    [[0 0 0] [0 0 0] [0 0 0]]
]

Guess you like

Origin www.cnblogs.com/bowendown/p/12595100.html