"Go Language" "6" Go language array

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

Listen to the "4" Go language type and add to type method, said go language in addition to the underlying types (such as int, float64, complex128, etc.), there is a composite type, which contains an array of this article. For the array everyone is not unfamiliar, in C language can declare a one-dimensional array: int arr[10], then the go language is how to define it?

First, the declaration of the array

1, the declaration format of the array is var arrname [Num]type, for example:

var Strarr [10]string Listen//Declare a one-dimensional array of strings consisting of 10 strings

var Bytearr [32]byte Listen//Declare a one-dimensional byte array consisting of 32 bytes

var Pointarr [12]*float64 Listen//Declare a one-dimensional array of pointers consisting of 12 pointers to float64 types

var Twoarrs [3][5]int Listen//Declare a two-dimensional int array consisting of 15 int elements

The reader may have found that the array was declared with the size of the array, and subsequent array sizes cannot be modified


2, similar to go basic type, after the array declaration is also given by go to default values

Its default value is determined by the array element type, the default value of int is 0, the default value of string is a null string, and the default value of the pointer is nil, where nil is similar to Java's null:)


Ii. Initialization of arrays

The array is initialized in the form of var arrname [num]type = [Num]type{value, value, value,....}, for example:

var Strarr [6]string = [6]string{"A", "B", "C", "D", "E", "F"}

var Bytearr [5]byte = [5]byte{32, 23, 42, 26, 21}

var Floatarr [3]float64 = [3]float64{3.1415, 2.6728, 1.4114}

var Twoarrs [3][5]int = [3][5]int{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}

The initial value explicitly specifies the number of array elements


is it possible to remove the number of array elements at initialization time? The answer is yes, but the number of elements must be changed to ... , as shown below:

var Strarr [6]string = [...] String{"A", "B", "C", "D", "E", "F"}

var Bytearr [5]byte = [...] Byte{32, 23, 42, 26, 21}

var Floatarr [3]float64 = [...] float64{3.1415, 2.6728, 1.4114}

var Twoarrs [3][5]int = [...] [5]int{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}

The reader estimates that the value 5 in the second [] in the two-dimensional array has not been omitted as ..., otherwise the use of the [...] array outside of the array literal error prompt appears


Can you put [...] Type is also removed? The answer is NO! But you can abbreviate the array definition to:

Strarr: = [...] String{"A", "B", "C", "D", "E", "F"}

Bytearr: = [...] Byte{32, 23, 42, 26, 21}

Floatarr: = [...] float64{3.1415, 2.6728, 1.4114}

Twoarrs: = [...] [5]int{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}


Iii. declaring an array with new

In addition to declaring an array in the above way, you can also declare an array using new

Strarr: = [3]string{"Shandong", "corrected brother", "funny"} //Normally initialize array form


Newstrarr: = new ([3]string) //Use new to declare an array

Newstrarr[0] = "Shandong" Listen //Assign a value to the array of the new declaration

NEWSTRARR[1] = "corrected brother"

NEWSTRARR[2] = "funny"

The reader may have found an array using the new declaration, preceded by A & symbol in front of the print, which indicates that the array using the new declaration returns a pointer to the array, which can be used to assign a value to a set of elements, as shown below:


Iv. Comparison of arrays

Intarr: = [2]int{1, 2}

Strarr: = [2]string{"Qing", "Ke"}

Fmt. Println (intarr = = Strarr)

It is obvious that an array of integers and arrays of strings cannot be compared, and this throws mismatched types [2]int and [2]string exception


So change to the following code?

INTARR1: = [2]int{1, 2}

INTARR2: = [3]int{1, 2, 3}

Fmt. Println (intArr1 = = INTARR2)

Although all arrays are integers, one is an array of 3 elements, one is an array of 2 elements, and the go language considers this to be two different types, which throws mismatched types [2]int and [3]int exception


Change the length of the array to 2 elements, but the element values are different?

INTARR1: = [2]int{1, 2}

INTARR2: = [2]int{1, 3}

Fmt. Println (intArr1 = = INTARR2)

The go language now considers intArr1 and INTARR2 to be the same type, but prints false because their element values are different


Modify the code below to print true

INTARR1: = [2]int{1, 2}

INTARR2: = [2]int{1, 2}

Fmt. Println (intArr1 = = INTARR2)


In other languages the array is not supported by = = compared to! =, only the wayward language of Go is available:)


Five, re-discussion of the initialization of arrays

"Example 1":

Intarr: = [10]int{1,2}

Fmt. Println (Intarr)

The above declares that the array has a length of 10, but there are only 2 values when initializing, which will not be an error at this time? The answer is no, go will automatically assign the following element to the default value 0


"Example 2":

Intarr: = [10]int{1:10, 2:20, 3:30, 4:40, 9:90}

Fmt. Println (Intarr)

This array is initialized in much the same way as JSON, which specifies that the 2nd element has a value of 10, the 3rd element has a value of 20, the 4th element has a value of 30, the 10th element has a value of 90, and the other element value is not assigned a value, so the default value is used



The array as a function parameter is a value pass

Package Main


Import "FMT"


Func Test (param [10]int) {

Listen to the FMT. Printf ("In Test function"-The address is:%p\n ", &param) //print the addresses of an integral array of parameters

}


Func Main () {

Listen Intarr: = [10]int{5:10} //Declare and initialize an array of integers

Listen to the FMT. PRINTF ("In main function ' The address is:%p\n", &intarr) ///print this integer array of addresses

Listen to test (intarr) //Call the test function

}

From the running results, the initialized array address in the main function is 0xc08200a1e0, and the array is passed to the test () function, and the address of the parameter printed in the test () function is found: The entry address is 0xc08200a230 and main () The array initialized in is not the same, indicating that the array is a value pass in the Go language, not a C-language address pass, which also means that modifying a parameter group in the test () method does not affect the element value of the array in main ()


Seven, array length function

The go language contains the Len () function, which is used to read the length of an array, using the following example:

Intarr: = [10]int{5:10}

Fmt. Println (len (intarr))

This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1616321

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.