Go language type: array

Source: Internet
Author: User
Tags array definition
This is a creation in Article, where the information may have evolved or changed.

In the go language, an array of arrays is a set of ordered elements of a specific length.

The array type of go consists of two parts-type and length, both of which are indispensable. An array is a contiguous memory space that stores elements of the same type, so determining the type of an array inevitably requires determining the type of storage elements and how many elements are stored.

In the Go language, the array length cannot be changed after it is defined .

An array is a value type, and each pass produces a copy.

Example:

Package Mainimport ("FMT" "StrConv") func main () {var a [5]int           //define array of 5 integer FMT. Println ("EMP:", a)//integer 0 value is 0a[4] = 100//modify array FMT. Println ("Set:", a) fmt. Println ("Get:", a[4])//Output single value FMT. Println ("Len:", Len (a))//Array length b: = [5]int{45, 32, 12, 42, 55}//define array value for array definition fmt. Println ("DCL:", b) for z, y: = range B {//traversal array FMT. Println ("ran:", Z, y)}var Twod [2][3]string    //two-D array for I: = 0; I < 2; i++ {//loop for J: = 0; J < 3; J + + {Twod[i][j] = StrConv. Itoa (i) + "-" + StrConv. Itoa (j)//modify array}}fmt. Println ("2d:", Twod)}

Output:

EMP: [0 0 0 0 0]set: [0 0 0 0 100]GET:100LEN:5DCL: [55]ran:0 45ran:1 32ran:2 12ran:3 42ran:4 552d:  [[0-0 0-1 0-2] [1-0 1-1 1-2]]

Arrays of several initialization methods:

var a [10]intvar a = [10]int{0,1,2,3,4,5,6,7,8,9}var a = [...] Int{0,1,2,3,4,5,6,7,8,9}var a = [2][2]int{[2]int{1,1}, [2]int{2,2}}var a = [2][2]int{{1,1}, {2,2}}

  

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.