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?
- 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?
- 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]
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?
- Justicearray = [3]string{}
- Fmt. Printf ("The Justice League is:%v\n", Justicearray)
- Output:
- 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?
- Justicearray = [...] string{"Superman", "Batman", "Wonder Woman"}
- Fmt. Printf ("The Justice League is:%v\n", Justicearray)
- Output:
- 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?
- Avengersarray: = [...] string{"Captain America", "Hulk"}
- Fmt. Printf ("The Avengers is:%v\n", Avengersarray)
- Output:
- 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?
- Newavengers: = Avengersarray
- Newavengers[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]
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.