This article introduces arrays and their basic usage in Go.
Array
An array is a collection of elements of the same data type. In Go, an array’s size is determined when it is declared. Its elements can be modified, but its size cannot be changed. The basic syntax is:
1
2
| // Defines an array with a length of 3 elements type atinta
var a [3]int
|
Array Definition
For example: var a [5]int. The length of an array must be a constant, and the length is part of the array’s type. Once defined, the length cannot be changed. [5]int and [10]int are different types.
1
2
3
| var a [3]int
var b [4]int
a = b //You can't do this because at this point a and b are different types
|
Array elements can be accessed by index. Indexes start at 0, and the index of the last element is len-1. Accessing an index outside the valid range causes an out-of-bounds access and triggers a panic.
Array Initialization
There are several ways to initialize an array.
Method 1
When initializing an array, you can use an initializer list to set the values of its elements.
1
2
3
4
5
6
7
8
| func main() {
var testArray [3]int //Numerical arrays will be initially converted to zero values of the int type
var numArray = [3]int{1, 2} //Use specified initial value to complete initialization
var cityArray = [3]string{"北京", "上海", "深圳"} //Use specified initial value to complete initialization
fmt.Println(testArray) //[0 0 0]
fmt.Println(numArray) //[1 2 0]
fmt.Println(cityArray) //Shenzhen, Shanghai
}
|
Method 2
With the method above, you must ensure that the number of initial values matches the array length. Generally, you can let the compiler infer the array length from the number of initial values. For example:
1
2
3
4
5
6
7
8
9
10
| func main() {
var testArray [3]int
var numArray = [...]int{1, 2}
var cityArray = [...]string{"北京", "上海", "深圳"}
fmt.Println(testArray) //[0 0 0]
fmt.Println(numArray) //[1 2]
fmt.Printf("type of numArray:%T\n", numArray) //type of numArray:[2]int
fmt.Println(cityArray) //Shenzhen, Shanghai
fmt.Printf("type of cityArray:%T\n", cityArray) //type of cityArray:[3]string
}
|
Method 3
You can also initialize an array by specifying values at particular indexes. For example:
1
2
3
4
5
| func main() {
a := [...]int{1: 1, 3: 5}
fmt.Println(a) // [0 1 0 5]
fmt.Printf("type of a:%T\n", a) //type of a:[4]int
}
|
Iterating Over an Array
There are two ways to iterate over array a:
1
2
3
4
5
6
7
8
9
10
11
12
| func main() {
var a = [...]string{"北京", "上海", "深圳"}
// Method 1: For cycle through
for i := 0; i < len(a); i++ {
fmt.Println(a[i])
}
// Method 2:forrange
for index, value := range a {
fmt.Println(index, value)
}
}
|
Multidimensional Arrays
Go supports multidimensional arrays. Here, we use a two-dimensional array as an example—an array containing nested arrays.
Defining a Two-Dimensional Array
1
2
3
4
5
6
7
8
9
| func main() {
a := [3][2]string{
{"北京", "上海"},
{"广州", "深圳"},
{"成都", "重庆"},
}
fmt.Println(a) //[Beijing Shanghai] [ Guangzhou Shenzhen] [Chengdu Chongqing]
fmt.Println(a[2][1]) //Support indexing values: Chongqing
}
|
Iterating Over a Two-Dimensional Array
1
2
3
4
5
6
7
8
9
10
11
12
13
| func main() {
a := [3][2]string{
{"北京", "上海"},
{"广州", "深圳"},
{"成都", "重庆"},
}
for _, v1 := range a {
for _, v2 := range v1 {
fmt.Printf("%s\t", v2)
}
fmt.Println()
}
}
|
Output:
1
2
3
| Beijing Shanghai
Guangzhou Shenzhen
Chengdu!
|
Note: For multidimensional arrays, only the first dimension can use ... to let the compiler infer the array length. For example:
1
2
3
4
5
6
7
8
9
10
11
12
| //Supported Writing
a := [...][2]string{
{"北京", "上海"},
{"广州", "深圳"},
{"成都", "重庆"},
}
//The inner layer of multi-dimensional arrays is not supported...
b := [3][...]string{
{"北京", "上海"},
{"广州", "深圳"},
{"成都", "重庆"},
}
|
Arrays Are Value Types
Arrays are value types. Assigning or passing an array as an argument copies the entire array. Therefore, modifying the copy does not affect the original array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| func modifyArray(x [3]int) {
x[0] = 100
}
func modifyArray2(x [3][2]int) {
x[2][0] = 100
}
func main() {
a := [3]int{10, 20, 30}
modifyArray(a) //Modivy modified by a copy x
fmt.Println(a) //[10 20 30]
b := [3][2]int{
{1, 1},
{1, 1},
{1, 1},
}
modifyArray2(b) //Modivy modified by a copy of bx
fmt.Println(b) //[[1 1] [1 1] [1 1]]
}
|
Notes:
- Arrays support the
== and != operators because their memory is always initialized. [n]*T represents an array of pointers, while *[n]T represents a pointer to an array.
Exercises
- Calculate the sum of all elements in the array
[1, 3, 5, 7, 8]. - Find the indexes of two elements in an array whose sum equals a specified value. For example, in the array
[1, 3, 5, 7, 8], the indexes of the two elements whose sum is 8 are (0,3) and (1,2).