[Golang] Maybe there's something you don't know, array and slice (1)

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

Array in the Golang

In Golang, an array is an ordered arrangement of elements of the same type, and the length cannot be changed to occupy a contiguous amount of space on memory.

1) Basic

First look at the declaration of the array: [Plain]View Plaincopyprint?
    1. var Justicearray [3]string
var Justicearray [3]string
 The above declares that Justicearray is a string array with 3 elements, the numbers inside the parentheses are necessary and cannot be omitted.
It is also explained that [3]string and [2]string are two different types of arrays.
Now assign a value to it:
[Plain]View Plaincopyprint?
    1. Justicearray = [3]string{"Superman", "Batman", "Wonder Woman"}
    2. Fmt. Printf ("The Justice League is:%v\n", Justicearray)
    3. Output:
    4. The Justice League is: [Superman Batman Wonder Woman]
Justicearray = [3]string{"Superman", "Batman", "Wonder Woman"}fmt. Printf ("The Justice League is:%v\n", Justicearray) output: The Justice League is: [Superman Batman Wonder Woman]
If you only want to create an array that populates the default values, you can:
[Plain]View Plaincopyprint?
    1. Justicearray = [3]string{}
    2. Fmt. Printf ("The Justice League is:%v\n", Justicearray)
    3. Output:
    4. The Justice League is: []
Justicearray = [3]string{}fmt. Printf ("The Justice League is:%v\n", Justicearray) output: The Justice League are: [  ]
The current array has 3 empty strings.
In addition you can use an ellipsis form:
[Plain]View Plaincopyprint?
    1. Justicearray = [...] string{"Superman", "Batman", "Wonder Woman"}
    2. Fmt. Printf ("The Justice League is:%v\n", Justicearray)
    3. Output:
    4. The Justice League is: [Superman Batman Wonder Woman]
Justicearray = [...] string{"Superman", "Batman", "Wonder Woman"}fmt. Printf ("The Justice League is:%v\n", Justicearray) output: The Justice League is: [Superman Batman Wonder Woman]
Use... Instead of numbers, the elements in curly braces need to match the length of the array you declare.
For the same purpose, the following declaration assignment is more concise:

[Plain]View Plaincopyprint?
    1. Avengersarray: = [...] string{"Captain America", "Hulk"}
    2. Fmt. Printf ("The Avengers is:%v\n", Avengersarray)
    3. Output:
    4. The Avengers is: [Captain America Hulk]
Avengersarray: = [...] string{"Captain America", "Hulk"}fmt. Printf ("The Avengers is:%v\n", Avengersarray) output: The Avengers is: [Captain America Hulk]
The return type to the right of the equals sign is [2]string.

2) Copying of arrays

One thing to emphasize is that a variable of type array refers to the entire array variable (unlike the array in C, which is a pointer to the first element of the array);
  Similar to the basic types of int, when assigning a variable of type array, the entire array is copied, referring to the following example:

[Plain]View Plaincopyprint?
    1. Newavengers: = Avengersarray
    2. Newavengers[0] = "Spider-man"
    3. Fmt. Printf ("The Old Avengers:%v\n", Avengersarray)
    4. Fmt. Printf ("The New Avengers:%v\n", newavengers)
    5. Output:
    6. The Old Avengers: [Captain America Hulk]
    7. The New Avengers: [Spider-man Hulk]
Newavengers: = avengersarraynewavengers[0] = "Spider-man" FMT. Printf ("The Old Avengers:%v\n", Avengersarray) fmt. Printf ("The New Avengers:%v\n", newavengers) output: The Old Avengers: [Captain America hulk]the New Avengers: [Spider-man Hulk ]
When the Avengersarray is assigned to Newavengers, the entire array is copied instead of simply pointing.
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.