Go Arrays
# Go Arrays
Go provides an array data structure.
An array is a sequence of numbered, fixed-length data items of the same unique type. This type can be any primitive type such as an integer, string, or a custom type.
Compared to declaring variables like **number0, number1, ..., number99**, using the array form **numbers, numbers ..., numbers** is more convenient and extensible.
Array elements can be read (or modified) via an index (position). The index starts from 0, the first element has index 0, the second has index 1, and so on.
!(#)
* * *
## Declaring Arrays
Declaring an array in Go requires specifying the element type and the number of elements. The syntax is as follows:
var arrayName dataType
Where **arrayName** is the name of the array, **size** is the size of the array, and **dataType** is the data type of the elements in the array.
The following defines an array named `balance` of length 10 and type `float32`:
var balance float32
* * *
## Initializing Arrays
The following demonstrates array initialization:
The following example declares an integer array named `numbers` with a size of 5. During declaration, each element in the array is initialized to its default value based on its data type. For integer types, the initial value is 0.
var numbers int
You can also initialize the array elements using an initializer list:
var numbers = int{1, 2, 3, 4, 5}
The above code declares an integer array of size 5 and initializes its elements to 1, 2, 3, 4, and 5 respectively.
Alternatively, you can use the short declaration syntax `:=` to declare and initialize an array:
numbers := int{1, 2, 3, 4, 5}
The above code creates an integer array named `numbers`, sets its size to 5, and initializes the element values.
**Note:** In Go, the size of an array is part of its type, so arrays of different sizes are incompatible. This means `int` and `int` are different types.
The following defines an array named `balance` of length 5 and type `float32`, and initializes its elements:
var balance = float32{1000.0, 2.0, 3.4, 7.0, 50.0}
We can also quickly initialize the array while declaring it using a literal:
balance := float32{1000.0, 2.0, 3.4, 7.0, 50.0}
If the array length is unknown, you can use `...` in place of the length, and the compiler will infer the array length based on the number of elements:
var balance = [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
or
balance := [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
If the array length is set, you can also initialize elements by specifying their indices:
// Initialize elements at index 1 and 3
balance := float32{1:2.0, 3:7.0}
The number of elements within the `{}` during initialization cannot be greater than the number in `[]`.
If you omit the number in `[]` and do not set the array size, Go will set the array size based on the number of elements:
balance = 50.0
The above example reads the fifth element. Array elements can be read (or modified) via an index (position). The index starts from 0, the first element has index 0, the second has index 1, and so on.
!(#)
* * *
## Accessing Array Elements
Array elements can be read via an index (position). The format is the array name followed by square brackets containing the index value. For example:
var salary float32 = balance
The above example reads the value of the 10th element in the `balance` array.
The following demonstrates a complete array operation (declaration, assignment, access) example:
## Example 1
package main
import "fmt"
func main() {
var n int /* n is an array of length 10 */
var i, j int
/* Initialize elements of array n */
for i = 0; i < 10; i++ {
n = i + 100 /* Set element to i + 100 */
}
/* Output the value of each array element */
for j = 0; j < 10; j++ {
fmt.Printf("Element[%d] = %dn", j, n)
}
}
The execution result of the above example is as follows:
Element = 100
Element = 101
Element = 102
Element = 103
Element = 104
Element = 105
Element = 106
Element = 107
Element = 108
Element = 109
## Example 2
package main
import "fmt"
func main() {
var i, j, k int
// Declare and quickly initialize an array
balance := float32{1000.0, 2.0, 3.4, 7.0, 50.0}
/* Output array elements */
for i = 0; i < 5; i++ {
fmt.Printf("balance[%d] = %fn", i, balance)
}
balance2 := [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
/* Output the value of each array element */
for j = 0; j < 5; j++ {
fmt.Printf("balance2[%d] = %fn", j, balance2)
}
// Initialize elements at index 1 and 3
balance3 := float32{1:2.0, 3:7.0}
for k = 0; k < 5; k++ {
fmt.Printf("balance3[%d] = %fn", k, balance3)
}
}
The execution result of the above example is as follows:
balance = 1000.000000
balance = 2.000000
balance = 3.400000
balance = 7.000000
balance = 50.000000
balance2 = 1000.000000
balance2 = 2.000000
balance2 = 3.400000
balance2 = 7.000000
balance2 = 50.000000
balance3 = 0.000000
balance3 = 2.000000
balance3 = 0.000000
balance3 = 7.000000
balance3 = 0.000000
* * *
## More Content
Arrays are very important in Go. Below, we will introduce more content about arrays:
| Content | Description |
| --- | --- |
| (#) | Go supports multi-dimensional arrays. The simplest multi-dimensional array is a two-dimensional array. |
| (#) | You can pass array parameters to functions. |
YouTip