Arrays in the Golang

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

Concept: Refers to a collection of data of the same type in a series.


The Declaration method is as follows:

[32]byte Listen, listen, hear, listen, listen, listen, listen, hear, hear, listen, heard, listen, hear, listen, listen, hear and hear.//array of length 32

[2*n] struct {x, y Int32} Listen//Complex Type array

[1000]*float64 Listen, listen, hear, hear, listen, hear, hear, hear, listen.//Pointer array

[3] [5]int Listen, listen, listen, listen, hear, listen, hear, hear, hear, hear, listen, and listen.//two-D arrays

[2] [2] [2]float64 Listen, listen, listen, listen, listen, hear, hear, listen,//equate to [2] ([2] ([2]float64)]


Get array Length: arrlength: = Len (arr)


The traversal array method is as follows:

Method One:

For i:=0; I<len (array); i++ {

Hear and hear the FMT. Println ("Element", I, "of array is", Array[i])

}

Method Two:

For I, V: = range Array {

Hear and hear the FMT. Println ("Array element[", I, "]=", V)

}


Special attention:

In Golang, the array is a value type. If you use an array as the parameter type for a function, the parameter will occur when the function is called for data replication. Therefore, the contents of the incoming array cannot be modified in the body of the function, because only one copy of the incoming array is manipulated inside the function.

Package Listen Mainimport Listen to "FMT" Func Listen to modify (array listen [5]int) listen {Array[0] listen = Listen to 10 listen to//try listen to modify listen to the first listen to the element listen to the ARRAYFMT . Println ("In Listen modify (), listen to array listen to values:", listen to array)}func listen to main () Listen {Array listen: = Listen [5]int{1, listen to 2, listen to 3, listen 4, listen to 5} Listen// Define listen and listen to the init listen to a listen to Arraymodify (array) Listen listen hear listen hear hear listen listen listen to hear listen to listen to listening to hearing a listen to a listening function, And listen try listen to listen to modify listen to the content listen to hear this arrayfmt. Println ("In Listen to Main (), listen to array listen to values:", listen to Array)}

Execution Result:

In modify (), array values: [10 2 3 4 5]

In main (), array values: [1 2 3 4 5]

As you can see from the execution results, the array in which the function modify () operates is two different instances from the array passed in main ().

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.