Overview
existPrevious SectionIn the content, we introduce the basic data types of Go, including: Boolean type, integer type, floating point type, complex type, string type, etc. In this section, we will introduce Go's array. An array in Go is a fixed-length data structure that contains a set of elements arranged in order, each of which has the same type. The elements of an array can be of any type, including primitive and composite types.
Declare an array
Declare an array to use the var keyword and specify the length and element type of the array at the same time. The syntax format is as follows:
var arrayName [size]dataType
Where, arrayName is the name of the array, size is the size of the array, and dataType is the data type of the element in the array.
In the following example code, we declare an array containing 3 string elements and an array containing 6 integer elements.
var arrText [3]string var arrNum [6]int
Note: When an array is declared, each element in the array is initialized by default according to its data type. For integer types, the initial value is 0; for string types, the initial value is an empty string.
Initialize the array
Initialization arrays can specify the value of an array element by initializing a list. Initialization list uses braces {} to contain array elements. Please refer to the following example code.
package main import "fmt" func main() { var arrNum [5]int = [5]int{1, 2, 3, 4, 5} // Output: [1 2 3 4 5] (arrNum) }
When assigning a value to the initialization list, you can specify only the values of some elements, and the remaining elements are initialized to 0 or empty strings by default. In the following example code, we declare an integer array with 5 elements, but only give the values of the first three elements. At this time, the value of the last two elements is 0.
package main import "fmt" func main() { var arrNum [5]int = [5]int{1, 2, 3} // Output: [1 2 3 0 0] (arrNum) }
You can also not specify the size of the array, but let the compiler automatically infer based on the initialization list. At this time, no number can be specified in [], or symbols... can be used instead of numbers.
package main import "fmt" func main() { arrNum := []int{1, 2, 3} // Output: [1 2 3] (arrNum) arrNum2 := [...]int{1, 2, 3, 4, 5} // Output: [1 2 3 4 5] (arrNum2) }
Access array elements
You can use indexes with brackets [] to access elements in an array. The index starts from 0 and increments in turn. For example: To access the first element of the array arr, you can use arr[0].
package main import "fmt" func main() { arrNum := []int{1, 2, 3, 4, 5} for i := 0; i < len(arrNum); i++ { (arrNum[i]) } }
Of course, you can also modify elements in the array, please refer to the following example code.
package main import "fmt" func main() { arrNum := []int{1, 2, 3, 4, 5} // Modify array elements, output: 101 102 103 104 105 for i := 0; i < len(arrNum); i++ { arrNum[i] += 100 ("%d ", arrNum[i]) } }
This is the end of this article about the specific use of Go arrays. For more related Go array content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!