Go language data types-arrays (array)

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

1 Introduction to the Go language array

Arrays are one of the most commonly used data structures in go language programming. As the name implies, an array refers to a collection of data of the same type. Each data contained in an array is called an array element, and the number of elements contained in an array is called the length of the array.

In the go language, an array is a value type (value). is a real-life array, not a pointer to the starting position of the array's memory, or a conversion with a pointer of the same type, which is significantly different from the C language. All value-type variables produce a copy action when they are assigned and passed as arguments. 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.

2 Array Definitions

defined in the following way

var arr [N]type
n represents the array length, and type represents the array storage type.

In the go language, the length of an array cannot be changed after it is defined, and the length can be a constant or a constant expression when declared (a constant expression is an expression that evaluates the result at compile time). The length of the array is a built-in constant for the array type, which can be obtained using the go language's built-in function Len ().

Arrlength: = Len (arr)

3 array declarations

General Declaration methods

var a [5]byte///array of length 5, each element is a byte var b [2*n] struct {x, y int5}//Complex type array var c [5]*int//pointer array var d [2][3]int//two-D array var e [2] [3] [4]int//equivalent to [2] ([3] ([4]int)]

4 Array Initialization

3.1 Declaring and initializing first

3.2 Declaring and initializing directly

A: = [3]byte{' 1 ', ' 2 ', ' 3 '}//Declaration and initialization of a byte array of length 3 A: = [...] byte{' 1 ', ' 2 ', ' 3 '}//can omit length and adopt ' ... ' way, go will automatically calculate the length according to the number of elements d: = [2][3]int{[3]int{1,2,3},[3]int{4,5,6}}d: = [2][3]int{{ 1,2,3},{4,5,6}}//If the inner element is the same as the outer one, then the above declaration can be simplified, ignoring the internal type directly

3.3 For example:

Package Mainimport ("FMT") func main () {    var arr1 [5]int    arr2: = [5]int{1, 2, 3, 4, 5}   //Specify a length of 5 and assign 5 initial values    ARR3: = [5]int{1, 2, 3}         //Specify a length of 5, assign a value to the first 3 elements, the other element is a zero value    arr4: = [5]int{4:1}            //Specify a length of 5, assign a value to the 5th element    ARR5: = [...] ]int{1, 2, 3, 4, 5}//unspecified length, assigning an array of 5 values    arr6: = [...] INT{8:1}          //Does not specify a length and assigns a value of 1 fmt to the 9th element (subscript 8)    . Println (arr1, arr2, ARR3, ARR4, ARR5, ARR6)}
The output is:

[Root@localhost mygo]# go run test.go [0 0 0 0 0] [1 2 3 4 5] [1 2 3 0 0] [0 0 0 0 1] [1 2 3 4 5] [0 0 0 0 0 0 0 0 1]

4 array element access

You can use array subscripts to access the elements in an array. array subscript starting from 0, Len (arr)-1 indicates the subscript of the last element

4.1 Calculating the length of an array

Via the Go language built-in function len

For example

Package Mainimport ("FMT") func main () {    arr: = [...] int {9:1}    fmt. Println (arr)    FMT. Println (Len (arr))}
The output is:

[Root@localhost mygo]# go run test.go [0 0 0 0 0 0 0 0 0 1]10

4.2 General access Mode

For I: = 0; I < Len (arr); i++ {    FMT. Println (i, Arr[i])}

For example:

Package Mainimport ("FMT") func main () {    arr: = [5]int {1, 2, 3, 4, 5} for    I: = 0; i < len (arr); i++{        FMT. Printf ("arr[%d]=%d\n", I, Arr[i])    }}
The printing results are as follows:

[Root@localhost mygo]# go run test.go arr[0]=1arr[1]=2arr[2]=3arr[3]=4arr[4]=5

4.3 Access by Range

For I, V: = range arr {    FMT. Println (i, v)}
Range has two return values, the first return value I is an array subscript for the element, and the second return value V is the value of the element.

For example:

Package Mainimport ("FMT") func main () {    arr: = [5]int {1, 2, 3, 4, 5} for    I, V: = Range (arr) {        fmt. Printf ("arr[%d]=%d\n", I, V)    }}

The printing results are as follows:

[Root@localhost mygo]# go run test.go arr[0]=1arr[1]=2arr[2]=3arr[3]=4arr[4]=5

5 Array Value passing

For example:

Package Mainimport ("FMT") func modify (arr [5]int) {   arr[0] = ten   fmt. Println ("in Modify (), arr values:", arr)}func main () {    arr: = [5]int{1, 2, 3, 4, 5}    Modify (arr)    FMT. PRINTLN ("in Main (), arr values:", arr)}
The output is:

[Root@localhost mygo]# Go run test.go in modify (), arr values: [2 3 4 5]in main (), arr values: [1 2 3 4 5]


< keywords: range >

**********************************************************************************************
Reprint Please specify source: http://blog.csdn.net/jesseyoung/article/details/36058095
**********************************************************************************************

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.