An array is a sequence composed of elements of a specific type with a fixed length. An array can be composed of zero or more elements.
1. Several ways to create an array
- Create with array length and specified data initialization
arr1 := [10]int {3,6,9,4,5,3,5,7,8,2} fmt.Println(arr1) // [3 6 9 4 5 3 5 7 8 2] fmt.Println(len(arr1)) // 10
- Created according to the number of initialization parameters
arr2 := [...]int {3,6,9,4,5,3,5,7,8} fmt.Println(arr2) // [3 6 9 4 5 3 5 7 8] fmt.Println(len(arr2)) // 9
- Initialize the element creation with the specified length 0 value. The first value specifies the length 0 value length number
arr3 := [...]int{10:2,-1} fmt.Println(arr3) // [0 0 0 0 0 0 0 0 0 0 2 -1] fmt.Println(len(arr3)) // 12 --> 10个0 + 后续两个数据
2. Array access
Each element of the array can be accessed through the index subscript. The range of the index subscript is from 0 to the position of the array length minus 1. You can use the Go built-in function len() to get the length of the data.
arr4 := [...]int{10: 2,-1}
fmt.Println(arr4[10])
// 2
Loop access of array
arr5 := [...]int{10: 2,-1}
for _, v := range arr5 {
fmt.Printf("%d\n", v)
}
3. Official instructions on array usage
- Arrays are values. Assigning one array to another copies all the elements.
- In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it.
- The size of an array is part of its type. The types
[10]int
and[20]int
are distinct. The size of an array is part of its type . [10] int type and [20] int type are different
- An array is a value object, and assigning an array to another array will copy all elements.
arr6 := [...]int{0,0,0} arr7 := arr6 arr7[2] = 2 fmt.Println(arr6) fmt.Println(arr7) // arr6 --> [0 0 0] // arr7 --> [0 0 2]
- If you pass an array to the function, it will receive a copy of the array instead of a pointer to the array.
If you want to pass by reference through an array, you need to pass the pointer type of the arrayarr8 := [2]int{1,2} fmt.Println(arr8) swap(arr8) fmt.Println(arr8) // [1 2] // [1 2] func swap(arr8 [2]int){ temp := arr8[0] arr8[0] = arr8[1] arr8[1] = temp }
arr8 := [2]int{1,2} fmt.Println(arr8) swap(&arr8) fmt.Println(arr8) func swap(arr8 *[2]int){ temp := arr8[0] arr8[0] = arr8[1] arr8[1] = temp } // [1 2] // [2 1]
- The size of the array is part of its type. [10] int type and [20] int type are different.
arr9 := [...]int{0,0,0}
arr9 = [...]int{0,0,0,0} // Cannot use '[...]int{0,0,0,0}' (type [4]int) as type [3]int
If the size (length) of the array is not constant, the compiler will directly prompt them that the type is different.
4. Extended description
- The problem of array comparison. Compare two arrays directly through the == comparison operator. The arrays are equal only when all elements of the two arrays are equal
arr10 := [3]int{1,1,1} arr11 := [3]int{1,1,1} fmt.Println(arr10 == arr11) // true arr12 := [3]int{1,1,1} arr13 := [...]int{1,1,2} fmt.Println(arr12 == arr13) // false
- Conversion of array and slice
arr14 := [10]int {3,6,9,4,5,3,5,7,8,2} // fmt.Println(append(arr14,10)) 会报错,Cannot use 'arr14' (type [10]int) as type []Type arr15 := arr14[:5] fmt.Println(arr15) arr16 := append(arr15,10) fmt.Println(arr16) // [3 6 9 4 5] // [3 6 9 4 5 10]
- This style isn't idiomatic Go. Use slices instead. Officially, this style of arrays is not idiomatic. It is recommended to use slices