Use of the Go Language array

Source: Internet
Author: User

Tag: The For loop sequence will populate the data type character code strong value

Go language Array

The Go language provides the data structure of the array type.

An array is a series of numbered and fixed-length data items that have the same unique type, which can be any primitive type, such as an integer, a string, or a custom type.

Relative to the declaration NUMBER0, Number1, ..., and number99 variables, using array form numbers[0], numbers[1] ..., numbers[99] more convenient and easy to expand.

Array elements can be read (or modified) by index (position), index starting at 0, index of first element is 0, second index is 1, and so on.

Declaring an array

The Go language Array declaration needs to specify the element type and the number of elements, the syntax format is as follows

var array_name [SIZE] Array_type

The above is how one-dimensional arrays are defined. The array length must be an integer and greater than 0. For example, the following defines an array balance length of 10 for type float32:

var blance [5] float32

If you do not give the initialized value, the default is to populate the elements of the array according to the data type, float32 the default padding of 5 large duck eggs ([0 0 0 0 0])
Initializing an array

The following shows the initialization of an array:

var blance = [5] float32 {1000.0, 2.0, 3.4, 7.0, 50.0}

After the initialized value is given, the elements in the array are [1000 2 3.4 7 50]

Attention:

    1. The number of elements in the {} initialization array cannot be greater than the number in [].
    2. If you ignore the number in [] without setting the array size, the Go language sets the size of the array according to the number of elements: [...]
Package Main Import (    "fmt") func  main ()  {    // will automatically calculate the number    of initialized  [...] float32 {1000.0, 2.0, 3.4, 7.0, 50.0}    //len function is an intrinsic function that prints the number    of elements in the initialized array Fmt. Println (Len (blance))}

Modifies the value of the first index in the array (the index of the array is calculated starting at 0)

BLANCE[1] = The second element in the 101.21//array is re-assigned to 101.21, and the result of the new array is [1000 101.21 3.4 7 50]

Access the values in the array (cannot exceed the number of arrays, or it will be an error)

func  Main ()  {    // will automatically calculate the number    of the initialization worth = [...] float32 {1000.0, 2.0, 3.4, 7.0, 50.0}    blance[1] = 101.21    //len function is an intrinsic function that prints the number of elements in the initialized array    FMT. Println (Len (blance))    FMT. Println (blance[4])    for 5 -element Array)    FMT. Println (blance[8])    */}

The following shows an example of an array full operation (declaration, assignment, access):

1 Package Main2 3 Import (4     "FMT"5 )6 7 Func Main () {8//Create a local array variable blance9var blance = [...] string {"a","b","C","D","e","F"}Ten//calculating the number of arrays OneCount: =Len (blance) A//Initializes an array of n -var n [6] int -      forI: = 0; I < count; i++{ the//assigns a value to the index of n through a for loop -N[i] = i + 10 -     } -      +//accessing the values inside the newly created n array -      forIndex,v: =Range n{ +Fmt. Printf ("the%d index in the n array is:%d \ n", Index,v) A     } at}

The result of the above code output is :

The No. 0 index in the n array is: 10
The 1th index in the n array is: 11
The 2nd index in the n array is: 12
The 3rd index in the n array is: 13
The 4th index in the n array is: 14
The 5th index in the n array is: 15

Go Language Multidimensional arrays

The Go language supports multidimensional arrays, and the following are common multidimensional array declarations:

var array_name [Size1][size2] ... [Sizen] Array_type
two-dimensional arrays

Two-dimensional arrays are the simplest multidimensional arrays, and two-dimensional arrays are essentially composed of one-dimensional arrays. The two-dimensional array is defined in the following way:

var arrayname [x] [y] Array_type

Array_type is the data type of the Go language, Arrayname is an array name, two-dimensional arrays can be considered a table, X is a row, Y is a column, and a two-dimensional array A is shown as three rows and four columns:

Elements in a two-dimensional array can be accessed through a[I [j] .

Initialize the two-bit array:

Func Main () {    // Initialize two-dimensional array     = [2][5] int {        {1,2,3,4,5},        {6,7,8,9,10 },    }    fmt. Println (Threedim)}

Gets the fourth value in the first element in the second-dimension array

Func Main () {    // Initializes a two-dimensional array, the index starts at 0, the first element is indexed to 0, the value    on the third index in the first element = [2][5] int {        { 1,2,3,4,5},        {6,7,8,9,10},    }    fmt. Println (threedim[0][3])}

The value on the corresponding index of a two-dimensional array with cyclic output

1 Package Main2 3 Import (4     "FMT"5 )6 7 Func Main () {8//initialize a two-dimensional array9var Threedim = [3][5] int {Ten{1,2,3,4,5}, One{6,7,8,9,10}, A{11,12,13,14,15}, -     } -Count: =Len (Threedim) the      forI: = 0; i< count; i++{ -Inner_count: =Len (threedim[i]) -          forJ: = 0; J < Inner_count; J + +{ -Fmt. Printf ("Threedim [%d][%d] =%d\n", I,j,threedim[i][j]) +         } -  +     } A}

The result returned by the above code is:

Threedim [0][0] = 1threedim [0][1] = 2Threedim [0][2] = 3Threedim [0][3] = 4Threedim [0][
    4] = 5Threedim [1][0] = 6Threedim [1][1] = 7Threedim [[] = 8Threedim [1][3] = 9< C8>threedim [1][4] = tenThreedim [2][0] = oneThreedim [2][1] = Threedim[2][2] = 13
    
     threedim [2][3] =
     Threedim [2][4] = 15
    

Use of the Go Language array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.