Go language-array type
An array is a container that can hold several types of elements of the same type. The size of the container (that is, the length of the array) is fixed and is reflected in the type literal of the array. For example, we declare an array type:
Type Mynumbers [3]int
Note: A type declaration statement consists of a keyword type , a type name, and a type literal.
The so-called type literal is used to represent a literal representation of a type (or notation method). In contrast, the literal representation of a value used to represent a type can be called a value literal, or simply a literal. For example, what has been mentioned before 3.7E-2 can be called floating-point number literals. The type literal consists of [3]int two parts. The first part is the length of the array wrapped in square brackets, that is [3] . This also means that the length of an array is part of the type of the array and is invariant. The second component of the type literal is int . It represents the type of element that the array can hold. In this case, the above-mentioned type declaration statement is actually [3]int an alias type declared for an array type. This allows us to use it as MyNumbers an array type [3]int .
When we represent a value of such an array type, we should write the type literal on the leftmost side, and enclose in curly braces the number of elements that the value contains. The elements are separated by commas (English half-width), i.e.:
[3]int{1, 2, 3}
Now, let's assign this array literal to a numbers variable named:
var numbers = [3]int{1, 2, 3}
Note: This is a variable declaration statement. It assigns a value to the variable at the same time it declares the variable.
Another convenient way to do this is to omit a number in the type literal that represents its length, like this:
var numbers = [...] Int{1, 2, 3}
This frees us from having to count the number of elements to fill in that number.
Next, we can easily use an index expression to access any element of the variable's value, for example:
NUMBERS[0]//will get the first element numbers[1]//will get a second element numbers[2]//will get the third element
Note: An index expression consists of a string, an array, a slice, or a value of a dictionary type (or a variable or constant that represents such a value), and an index value wrapped in square brackets. Here, the valid range of index values is [0, 3]. That is, for an array, the index value cannot be less than 0 or greater than or equal to the length of the array value. Also note that the minimum valid value for an index value is always 0, not 1.
In contrast, if we want to modify an element value in an array value, we can use an assignment statement to achieve it directly. For example, if we want to modify numbers the second element in, we can:
NUMBERS[1] = 4
Although the length of the array has been reflected in its type literal, we still need to get it clearly in many cases, like this:
var length = len (numbers)
Note: Is the name of the built- len in function of the go language. This function is used to get the length of a string, array, slice, dictionary, or channel type value. We can use it directly in the Go language source file.
Finally, note that if we only declare a variable of an array type without assigning a value to it, then the value of the variable will be an array value of the specified length, where each element is a 0 value (or default value) of the element type. For example, if you have such a variable:
var numbers2 [5]int
Then it will be a value of
[5]int{0, 0, 0, 0, 0}
Task
Please fill in a number in the index.go of the 11th line of the command source file, so that the program prints to the content on the standard output true .
Package Mainimport"FMT"Func Main () {varNumbers2 [5]intnumbers2[0] =2numbers2[3] = numbers2[0] -3numbers2[1] = numbers2[2] +5numbers2[4] =len (numbers2) sum:= ( One) //"= =" is used to determine the equality of two valuesFmt. Printf ("%v\n", (sum = = numbers2[0]+numbers2[1]+numbers2[2]+numbers2[3]+numbers2[4]))}
Go Language Learning 03