The array in Go

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

Today, let's take a look at the array in go. To understand the array in go, you need to know the other concept of go slice, and to know that slice you have to know the difference between new and make, and to know the difference between new and makes you have to understand the difference between value,pointer,reference. Let's take a look at these concepts.

1, value, pointer and reference

These concepts are actually the same as in other languages, but we are here to say that this is because the go language uses value, so if you assign an array to another array, it will inevitably involve a value copy, such as a: = [9]int{ 0,1,2,3,4,5,6,7,8} B: = A, then here B is copied to the value of a. On the contrary, there is no such copy for pointer and reference

2. New and make

In the go language, new, like other languages, allocates space for an instance of a type, initializes it to 0, and returns the address. Make is only for array,map and channel types, because these types need to be initialized or not, or, frankly, these types are not part of the basic data type, and when you declare an int type, go will assign you the space required for an int type. , but if you declare a []int, it cannot be assigned because it does not know how much space to allocate. The role of make is to allocate space and return a reference to that space. Note that this is a reference, not a pointer.

3, slice and array

For go, the size of the array is part of the group definition, [6]int and [5]int are different data types, so it's obvious that we need a uniform way to represent the array, so slice appears, slice's definition is simple []int means slice, Yes, don't be surprised that the so-called slice is an array that does not specify array size []interface. So how do you get a slice? There are two ways, one is make, such as do (int, 10, 100) to generate an array of type int, its size is 100, but currently only 10 is used. or a [6]int; A[0:5]; This can also get a slice.

Multi-dimensional arrays do not behave like C in the go, but rather like Java, which requires a one-dimensional one-dimension down.

For example a [9][9]int, in fact, represents an array a which has 9 elements, each of which is [9]int type].

Previously in the discussion in the mail, it was suggested that this representation is too cumbersome for multidimensional arrays, but the subsequent discussion shows that multidimensional implementations can make the whole language more complex if it is to support slice, so there is no need to specifically implement one such thing.

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.