This is the 11th chapter of Golang Language Learning Tutorial
Array
An array is a collection of elements of the same type. An array can consist of 0 or more elements.
To define the format of an array:
var <varName> [n]type , n >0
nRepresents the number of elements in an array, type representing the type of each element, and the number of elements n as the length of the array type, for example:
var a [2]int //数组 a 是长度为 2 的 int 型数组var b [2]int
And the go language does not allow mixing of elements of different types, such as arrays that contain strings and integers.
package mainimport "fmt"func main(){ var a [2]int // //定义一个长度为 2 的整数数组 fmt.Println(a)}
all of the elements in the array are automatically assigned an array of type 0 values , which a is an integer, so a all elements are assigned a value of int type 0:0. The result of the above program is[0 0]
The array is indexed from the 0 beginning, to the length -1 end, let's assign values to the above array:
package mainimport "fmt"func main(){ var a [2]int //定义一个长度为 2 的整数数组 a[0] = 45 a[1] = 25 fmt.Println(a)}
a[0]Assigns a value to the first element of the A array. The result of the above program is:[45 25]
Brief statement
package mainimport "fmt"func main(){ /*简略声明*/ b := [3]int{15, 35, 55} fmt.Println(b)}
The results of the above program run as follows:[15 35 55]
In a shorthand declaration, you do not need to assign values to all the elements in the array
package mainimport "fmt"func main(){ c := [4]int{12} fmt.Println(c)}
In the above program, c := [4]int{12} declare an array of length 4, but only provide a value of 12, the remaining three elements are automatically assigned to 0 .
The results of the above program run as follows:[12 0 0 0]
You can even ignore the length of the declared array, [...] instead, the compiler will automatically calculate the length:
package mainimport "fmt"func main(){ d := [...]int{1, 3, 5, 7} fmt.Println(d)}
Types of arrays
An array is a value type and not a reference type, and when an array is assigned to a new variable, the variable gets a copy of the array, and if changes are made to the new variable, the original array is not affected.
package mainimport "fmt"func main(){ e := [...]string{"BTC", "ETH", "EOS"} f := e f[0] = "QTUM" //更改数组 f 的第一个元素 fmt.Println("e is ", e) //对 数组 f 值修改不会影响到原始数组 fmt.Println("f is ", f) }
In the above program, will be e assigned to f , the f first element of the modification, will not affect the original array.
The results of the above program run as follows:
E is [BTC ETH EOS]
f is [QTUM ETH EOS]
Similarly, when an array is passed as a parameter to a function, they are passed by value, and the original array does not change:
package mainimport "fmt"func changeLocal(num [5]int) { //定义函数 changeLocal ,输入值 num 是一个长度为 5 的整数数组 num[0] = 55 //在函数中将输入值得第一个元素改为 55 fmt.Println("inside function ", num) }func main() { num := [...]int{5, 6, 7, 8, 8} //定义数组num值为{5, 6, 7, 8, 8} fmt.Println("before passing to function ", num) //正常输出数组 changeLocal(num) //调用函数输出数组,在期间改变了第一个元素为 55 fmt.Println("after passing to function ", num) //继续正常输出数组}
In the above program, 13 lines num are actually passed to the function by value changeLocal , and the original array does not change because of a function call.
The results of the above program run as follows:
Before passing to function [5 6 7 8 8]
Inside function [55 6 7 8 8]
After passing to function [5 6 7 8 8]
The length of the array
By passing an array to len a function, you can get the length of the array:
package mainimport "fmt"func main(){ /*数组的长度*/ g := [...]float64{55.3, 34, 0.92, 99.99, 88.8} fmt.Println("the length of g is ", len(g)) //通过len函数得到数组 g 的长度}
The results of the above program run as follows:the length of g is 5
Use range to iterate over algebraic groups
package mainimport "fmt"func main(){ /*使用range迭代数组*/ h :=[...]float64{55.3, 34, 0.92, 99.99, 88.8} //定义数组 h sum := float64(0) //定义变量 sum for i, v := range h{ //range返回两个值:索引和该索引处的值,第一次循环返回 0 和 55.3 ,分别赋值给 i 和 v fmt.Printf("\nthe %d value is %.2f", i, v) sum += v //将 v 的值加起来 } fmt.Println("\nsum of all values is", sum)}
The above procedure for i, v := range h is used for the For loop range method. It returns the index and the value at that index. We print the values and calculate the sum of all the elements in the array H.
The results of the above program run as follows:
The 0 value is 55.30
The 1 value is 34.00
The 2 value is 0.92
The 3 value is 99.99
The 4 value is 88.80
Sum of all values are 279.01
If you need only values and do not need an index, you can replace the index with a _ blank identifier:
for _, v := range h { // 忽略range的索引}
The For loop above ignores the index, and the same value can be ignored.
The above for learning Golang Array , the next section of the application of learning