First, declare and iterate
main Package Import " FMT " // declare an array. 1 var ARR [ . 3 ] int var arr2 is = [ . 4 ] int { . 1 , 2 , . 3 , . 4 } FUNC main () { // declare an array of 2 A: = [ . 4 ] float64 { 2.3 , . 4 , . 5 , 6.1 } fmt.Println (A) B: = [...] int { 2 , . 3 , . 4 } // iterate mode. 1 for I: = 0 ; I <len (A); I ++ { fmt.Print (A [I], " \ T " ) } fmt.Println () // iterate embodiment 2 for _, value: = Range B { fmt.Print (value, " \ T " ) } // IF ARR nil == { // fmt.Println ( "ARR == nil") // } fmt.Print ( " \ n-traversal arr array: " ) for _, value: = Range arr { fmt.Print (value, " \ T") } }
Arrays are passed by value
package main import "fmt" func main() { a := [...] string{"a" , "b" , "c" , "d"} b := a b[0] = "x" fmt.Println("a" , a)//[a b c d] fmt.Println("b" , b)//[x b c d] }
Second, the two-dimensional array
In the two-dimensional array of two-dimensional concept, which is to say in both directions index change, the index variable position in the array is also in a plane, rather than just one-dimensional array as a vector. However, the actual hardware is consecutively addressed memory, i.e. memory cells are one-dimensional linear arrangement. How to store in the memory a two-dimensional array dimension, there are two ways: one is arranged in rows, i.e., is done sequentially into the second line after line. Another is arranged in columns, that is done after a re-placed in a second row sequentially.
In the GO language, the two-dimensional array are arranged in rows. That is, to store a [0] lines, and then stored a [1] line, and finally storing a [2] line. The elements in each row is sequentially stored.
1, a two-dimensional array of elements is also called a dual subscript variable, which represents the form of:
array name [index] [index]
2, where the subscripts should be an integer constant or integer expression. For example:
a [. 3] [. 4]: an array of elements representing a three rows and four columns.
package main import "fmt" func main() { var a = [5][3]int{ {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12},{13,14,15}} for i:=0; i<len(a) ;i++ { for j:=0;j<len(a[0]) ;j++ { fmt.Printf("a[%d][%d]=%d\n" , i , j , a[i][j]) } } }