This is a creation in Article, where the information may have evolved or changed.
The go language array is a fixed-length sequence, and the array contains the same type of elements.
Package Mainimport "FMT" Func Main () {// Here we have created an integer (int) array with 5 elements //elements whose data type and array length are part of the arrays type //By default, The array elements are all 0 values. //For integers, a value of 0 is 0 var a [5]int FMT. Println ("EMP:", a) //We can use indexed values (index) to set the value of an array element, like so "array[index] = value"//or use an index to get the element value, such as "Array[index]" a [4] = the fmt. Println ("Set:", a) FMT. Println ("Get:", a[4]) //The built-in Len function returns the length of the array FMT. Println ("Len:", Len (a)) //Use the following methods to define and initialize an array of b: = [5]int{1, 2, 3, 4, 5} FMT. Println ("DCL:", b) ///arrays are one-dimensional, but you can define an array of elements as an array //To get a multidimensional array structure var twod [2][3]int for I: = 0; I < 2; i++ {for J: = 0; J < 3; J + + { Twod[i][j] = i + j } } fmt. Println ("2d:", Twod)}
Output
$ Go run arrays.goemp: [0 0 0 0 0]set: [0 0 0 0]get:100len:5DCL: [1 2 3 4 5]2d: [[0 1 2] [1 2 3]
When you use FMT. println the output array, you will find that the array will be [v1,v2,v3 ...] form is printed.
In the classic go language, you will encounter more slices (slice) than with arrays (array).
To learn more about arrays, see Learning Golang Language (5): Type--array
The next section will explain the slices.
Next example: Go by Example:slices.
English original