Array
An array is a collection of elements 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. (Translator's Note: Of course, if the interface {} is an array type, can contain any type)
Note: array definition phase, the length and type is fixed, and can not be changed later
Array Declaration
It is a representation of an array [n] T. n represents the number of elements in the array, T representative of the type of each element. Also part of the element n type .
Example: The simplest definition
package main import "fmt" func main() { A var [ 5 ] int # define an array of type int length of 5, it is 0 because there is no assignment int fmt.Println(a) }
# Results
[00000]
The definition and initial value
package main import "fmt" main FUNC () {
# three ways defined assignment, since only four values of the assignment, the final one of 0 var A [ . 5] int = [. 5] int {1,2,3,4 } # The first A var = [. 5] int {1,2,3,4 } # The second A: = [. 5] int {1,2,3,4 } # third A [ 2] = 100 # index value the modified data 100 2 fmt.Println(a) }
# The results are
[1210040]
Given the definition of a location assignment
Examples: set to position 5 to 99
package main import "fmt" func main() { A: = [10] int {1,2, four ninety-nine } # Note: go negative language does not support index array a [-2] Such fmt.Println(a) }
# Results
[12009900000]
Array is a value type
Go in the array are value types rather than reference types. This means that when the array is assigned to a new variable that will get a copy of the original array. If you make changes to a new variable, it will not affect the original array.
All parameters are passed copy transfer functions, parameter passing in the python is immutable value parameter passing, copies the value of the size, the type of the variable parameter is the address of a reference transmission, copies the value passed .
package main import "fmt" func main() { a:=[4]int{1,2,3,4} test6 (a) fmt.Println(a) } func test6(b [4]int){ B [0] = 100 data modification does not affect a #b fmt.Println(b) }
# Results
[100 2 3 4]
[1 2 3 4]
Length of the array
Len function used to obtain the length of the array
package main import "fmt" func main() { a:=[4]int{1,2,3,4} fmt.Println (referred to as (a)) } # Result 4
Array size is part of the type
var in [4] = int [4] int {1,2 ,} B var [ . 5] int = [. 5] int {4,5,}
two different array types, considering that the length of the sample sizes
With or (&& ||!)
a:=10 if !(a>9||a<6){ }
Use range iterative array
For loop through the array of elements
package main import "fmt" func main() { var a =[4]int{1,2,} for i:=0;i<len(a);i++{ fmt.Println(a[i]) } } # Results 1200
Go to iterate through the range provides a method for using a loop array. The return value of the range and the index of the index
Note: If the return value of the reception range with two: one is the index value, is a value. If only one receives the return value: it only shows the index
package main import "fmt" func main() { A var = [. 4] int {1,2 ,} for I, V: = Range A {# receiving if there are two values: a is an index, is a value. fmt.Println("------",i) fmt.Println (in) } }
Multidimensional Arrays
package main import "fmt" func main() { A var [ . 7] [2 ] int # 7 generated A [0] [ . 1] = 100 fmt.Println(a) } # Results [[0100] [00] [00] [00] [00] [00] [00]]