Learn what
What is an array?
How to get the array length?
How do I manipulate array elements?
How do I compare two arrays?
How do I copy arrays?
How to create a multidimensional array?
How to omit variables (supplementary)?
concept
Array is a collection with fixed length and the same element type, and the type can also be customized. If you want to save any type, define an array of interface types.
The position of each element of an array is called an index. The first element index is 0, the second is 1, and so on.
statement
Declaration format: var array name [length] type
Example: declare an array with length of 4 and element type of int.
var nums [4]int // Print [0 0 0 0]
After declaration, each element is initialized to 0 by default. If it is a string array, it is initialized to an empty string by default.
initialization
There are many initialization methods for arrays. List them one by one. I use the following: = abbreviation.
Method 1: initialize each element. If it exceeds the length of the array, an out of bounds error will be prompted during compilation.
nums := [4]int{3, 2, 1, 4}
Method 2: initialize some elements, only indexes 0 and 1 are assigned, and the following elements are 0 by default.
nums := [4]int{3, 2} // Print [3 2 0 0]
Method 3: initialize the specified index element in the form of key/value, or mix it with the above method.
nums := [4]int{0: 3, 3: 4} or nums := [4]int{3, 3: 4} // Print [3 0 0 4]
Method 4: without specifying the length of the array, use... To let the compiler decide the length of the array.
// The array length is 4 nums := [...]int{3, 2, 1, 4}
Gets the length of the array
Use the built-in function len to get the length of the array.
len(array)
It can also be used to obtain the length of slices, map s, strings and channels.
Array element operation
1. Get element
The index is used to obtain the elements of the array, and so is the slice acquisition method to be described later.
nums := [...]int{3, 2, 1, 4} fmt.Println(nums[1]) // output 2
2. Set element
When you have an array, you can modify the array elements later.
nums := [...]int{3, 2, 1, 4} nums[1] = 3 fmt.Println(nums) // output [3 3 1 4]
Traversal array
Use the "for loop statement" to traverse the array. The "for loop statement" is explained in detail in the previous "process control".
There are two ways to traverse the array. The first is "iteration count" and the second is "for range", which are used as follows:
1. Iteration count
nums := [...]int{3, 2, 1, 4} for i := 0; i < len(nums); i++ { fmt.Println(nums[i]) }
2. for-range
for i, v := range nums { fmt.Println("Indexes:", i, " value", v) } // output Index: 0 value 3 Index: 1 Value2 Index: 2 value 1 Index: 3 value 4
Array comparison
Whether two arrays are equal depends on two aspects:
Arrays must be of the same type. The type referred to here is not the element type, but the array length and element type determine the type of the array. For example: [2]int{1, 2} its type is [2]int, so don't mistake it for array.
Array elements are equal
a := [2]int{1, 2} b := [2]int{1, 2} if a == b { print("equal") } else { print("equal") } // output equal
Note: if the initialization mode of b is [...] int {1, 2}, a and b are still equal because the type is still [2]int.
Array copy
In Go language, an array is a value type, that is, it will be copied automatically during the transfer process.
nums := [...]int{3, 2, 1, 4} numsCopy := nums numsCopy[1] = 3 fmt.Println("nums:", nums) fmt.Println("numsCopy:", numsCopy) // output nums: [3 2 1 4] numsCopy: [3 3 1 4]
Multidimensional array
For a better understanding of multidimensional arrays, now restore a scene.
Now there is a classroom with 4 rows and 3 columns of seats. Now let you record the status of students in each seat. 0 means absenteeism, 1 means leave, and 2 means present.
1. Declaration
Let's first look at how multidimensional arrays are declared.
2D array format:
var Array name [length][length]type
3D array format:
var Array name [length][length][length]type
By analogy, you can continue to declare four-dimensional arrays, five-dimensional arrays, and so on.
Continuing back to the above scenario, you can determine that you need a two-dimensional array with 4 rows and 3 columns.
var students [4][3]int
3. Initialization
By setting the status of each student, it can be seen that 1 row and 3 columns of students are absent from class (value is 0), and 3 rows and 2 columns of students ask for leave (value is 1).
students := [4][3]int{ {2, 2, 0}, {2, 2, 2}, {2, 1, 2}, {2, 2, 2}, }
4. Traverse two-dimensional array
Use the "iteration count" method to traverse the students array and output the seats of students who are absent from class.
// collection/mult-array.go for i := 0; i < 4; i++ { for j := 0; j < 3; j++ { if students[i][j] == 0 { fmt.Printf("%d that 's ok%d List students absenteeism", i+1, j+1) } } } // output 1 Row 3 student absenteeism
Omitting variables
In Go language, there is a requirement for using variables: "in the function body, if the variables are declared, they must be used". If you don't want to use it, use "underline ()" to omit it.
For two examples, omit the b variable returned by the function and the index i of the array loop.
a, b := fun1() for i, v := range array { ... } // After omission a, _ := fun1() for _, v := range array { ... }
summary
This article explains the array in the built-in set of Go language and the supplement to omitting variable knowledge points, because it is too commonly used. The next chapter will explain slicing, which is closely related to arrays, so this chapter must be understood. If you encounter any problems, leave a message below!