English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The array in the Go programming language is very similar to other programming languages. In a program, we sometimes need to store a set of data of the same type, such as a list of student scores. This type of collection is stored in the program using an array. An array is a fixed-length sequence used to store elements of the same type in memory. Since their fixed length, arrays are not as popular as slices in the Go language.
In an array, it is allowed to store zero or more elements. By using the [] index operator and its zero-based position to index the elements of the array, this means the index of the first element isarray [0]the index of the last element isarray [len(array)]-1].
在Go语言中,数组是通过两种不同的方式创建的:
]Creating and accessing arrays
Syntax:
In Go language, arrays are created in two different ways: Or Using var keyword:1, item2, item3In Go language, create arrays using the var keyword with a specific type, name, and size.
Var array_name[length]Type
var array_name[length]Typle{item
, ...itemN}
Important note:
In Go language, arrays are mutable, so you can use the array[index] syntax on the left side of the assignment to set the array element at the given index.
Var array_name[index] = element[object Object]
You can access array elements using index values or using a for loop.
//In Go language, array types are one-dimensional. //The length of an array is fixed and cannot be changed. package main import "fmt" func main() { //You can store repeated elements in an array. Using var keyword to create an array3Using its index value to access the array //Using var keyword, create a string array ]string ] = "www.w1Assigning elements using index3myarr[0] = "GFG" ] = "www.w2codebox.com"3myarr[ //] = "w //codebox" fmt.Println("Array elements:") Using index value 1Accessing array elements Using index value 2fmt.Println("Element:", myarr[0])1: ", myarr[ Using index value 3fmt.Println("Element:", myarr[0])2: ", myarr[ {}
Output:
Array elements: : www.w 1) : www.w 2: GFG3codebox.com : www.w 3Element3codebox
: wIn Go language, arrays can also be declared using abbreviated syntax, which is more flexible than the above declaration.
Syntax:
array_name := [length]Type{item1, item2, item3,...itemN}[object Object]
//Using arrays with abbreviated declaration //Accessing arrays using a for loop package main import "fmt" func main() { //Abbreviated array declaration arr := [4]string{"w3codebox", "gfg", "w3codeboxs1231", "www.oldtoolbag.com" //Accessed element, using the array in the for loop fmt.Println("Array elements:") for i := 0; i < 3; i++ { fmt.Println(arr[i]) {} {}
Output:
Array elements: w3codebox gfg w3codeboxs1231
We already know that arrays are one-dimensional, but it is possible to create multidimensional arrays. Multidimensional arrays are arrays of arrays of the same type. In Go language, you can create multidimensional arrays using the following syntax:
Array_name[Length]1][Length2]..[LengthN]Type
You canUse Var keywordOrUse shorthand declarationTo create multi-dimensional arrays, as shown in the following examples.
Note:In multi-dimensional arrays, if the user does not initialize a cell with a value, the compiler will automatically initialize it to zero. There is no concept of uninitialized in Golang.
package main import "fmt" func main() { //Create and initialize //Two-dimensional array //Use shorthand declaration //Here you need to use (,) commas arr := [3][3]string{{"C#", "C", "Python"}, {"Java", "Scala", "Perl"}, {"C++", "Go", "HTML"}} //The value accessed //Array access using for loop fmt.Println("Array elements 1") for x := 0; x < 3; x++ { for y := 0; y < 3; y++ { fmt.Println(arr[x][y]) {} {} //Create a two-dimensional array //Use var keyword array //and initialize one //Use indexed multi-dimensional arrays var arr1 [2][2]int arr1[0][0] = 100 arr1[0][1] = 200 arr1[1[0] = 300 arr1[1][1] = 400 //Access the value of the array fmt.Println("Array elements 2") for p := 0; p < 2; p++ { for q := 0; q < 2; q++ { fmt.Println(arr1[p][q]) {} {} {}
Output:
Array elements 1 C# C Python Java Scala Perl C++ Go HTML Array elements 2 100 200 300 400
In an array, if the array is not explicitly initialized,This arrayofDefault value is 0.
package main import "fmt" func main() { //Create an int type array, storing two elements //Here, we do not initialize the array, so the value of the array is zero var myarr[2]int fmt.Println("Array element :", myarr) {}
Output:
Array element : [0 0]
In an array, you canUselen() methodGetArrayLength,As shown below:
package main import "fmt" func main() { //Create an array //Use shorthand declaration arr1 :=[3]int{9, 7, 6{} arr2 :=[...]int{9, 7, 6, 4, 5, 3, 2, 4{} arr3 :=[3]int{9, 3, 5{} // Use len method to calculate array size fmt.Println("Array1Length is:1)) fmt.Println("Array2Length is:2)) fmt.Println("Array3Length is:3)) {}
Output:
Array1Length is: 3 Array2Length is: 8 Array3Length is: 3
在数组中,If the ellipsis " ..."If the ellipsis is visible at the length position, the length of the array is determined by the initialized elements. For example:
//How to use ellipsis in an array package main import "fmt" func main() { //Create an array of fixed size //According to the number of elements in it //Use ellipsis myarray := [...]string{"GFG", "gfg", "w3codeboxs3codebox.com, "w"3codebox"} fmt.Println("数组元素: ", myarray) //数组的长度 //由...决定 //使用len()方法 fmt.Println("数组的长度为:", len(myarray)) {}
Output:
数组元素: [GFG gfg w3codeboxs www.oldtoolbag.com w3codebox] 数组的长度为: 5
在数组中,允许您在array 的元素范围内进行迭代。如下例所示:
//如何迭代数组 package main import "fmt" func main() { //创建一个数组,其大小 //用省略号表示 myarray := [...]int{29, 79, 49, 39, 20, 49, 48, 49{} //使用for循环迭代数组 for x := 0; x < len(myarray); x++ { fmt.Printf("%d\n", myarray[x]) {} {}
Output:
29 79 49 39 20 49 48 49
在Go语言中,数组的值类型不是引用类型。因此,当将数组分配给新变量时,在新变量中所做的更改不会影响原始数组。如下例所示:
package main import "fmt" func main() { //创建一个数组,其大小 //用省略号表示 my_array := [...]int{100, 200, 300, 400, 500} fmt.Println("原始数组(改变前):", my_array) //创建一个新变量 //并使用my_array进行初始化 new_array := my_array fmt.Println("新数组(改变前):", new_array) //将索引0处的值更改为500 new_array[0] = 500 fmt.Println("新数组(改变后):", new_array) fmt.Println("原始数组(改变后):", my_array) {}
Output:
原始数组(改变前): [100 200 300 400 500] 新数组(改变前): [100 200 300 400 500] 新数组(改变后): [500 200 300 400 500] 原始数组(改变后): [100 200 300 400 500]
在数组中,如果数组的元素类型是可比较的,则数组类型也是可比较的。因此,我们可以使用==运算符直接比较两个数组。如下例所示:
//如何比较两个数组 package main import "fmt" func main() { arr1 :=[3]int{9, 7, 6{} arr2 :=[...]int{9, 7, 6{} arr3 :=[3]int{9, 5, 3{} //使用==运算符比较数组 fmt.Println(arr1 == arr2) fmt.Println(arr2 == arr3) fmt.Println(arr1 == arr3) //这将给出和错误,因为 // arr1和arr4的类型不匹配 /* arr4:=[4]int{9,7,6{} fmt.Println(arr1==arr4) */ {}
Output:
true false false