Go by Example:arrays

Source: Internet
Author: User
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

Related Article

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.