An array of

An array of concepts

An array is a sequence of numbered, fixed-length items of the same unique type, which can be any primitive type such as integer, string, or custom type. Conceptually, arrays are basically the same in all languages. Note that once an array is defined, its size cannot be changed.

Related to the grammar

  • Declare and initialize an array
var variable_name [size] variable_type
Copy the code

The sample

var tmp [20] int
var tmp1 = [5] {1.2.3.4.5}
Copy the code

Note: the number of elements in the initialized array {} cannot be greater than the number in []. If you ignore the number in [] and do not set the size of the array, Go will set the size of the array based on the number of elements:

var tmp2 = []int{1.2.3.4.5.6}
len(tmp2)  // The output is 6
Copy the code

If you need to change a value in an array element:

tmp2[1] = 100
fmt.Println(tmp2) //[100 2 3 4 5 6]
Copy the code
  • Other ways to create arrays
var a [4] float32 Var arR2 = [4]float32{}
fmt.Println(a) // [0 0 0 0 0]
var c = [5] int{'A'.'B'.'C'.'D'.'E'} // byte
fmt.Println(c) // [65 66 67 68 69]
d := [...] int{1.2.3.4.5}// Set the size of the array based on the number of elements
fmt.Println(d)//[1 2 3 4 5]
e := [5] int{4: 100} // [0 00 0 100] Indicates that the storage value of the position with subscript 4 is 100
fmt.Println(e)
f := [...] int{0: 1.4: 1.9: 1} // [1 0 0 0 1 0 0 0 0 1]
fmt.Println(f)
Copy the code
  • How the elements of an array are accessed
int tn = tmp2[2] // to access the element of tmp2 array with subscript 2
Copy the code
var t [20] int
var t1 ,t2 int
for t1=0; t1<10; t1++ { t[t1] = t1*2
}
for t2 = 0; t2<10; t2++ { fmt.Printf("The [%d] number, value %d\n",t2,t[t2])
}
Copy the code

Code run result

The first"0】, the value is0The first"1】, the value is2The first"2】, the value is4The first"3】, the value is6The first"4】, the value is8The first"5】, the value is10The first"6】, the value is12The first"7】, the value is14The first"8】, the value is16The first"9】, the value is18
Copy the code
  • Gets the length of the array

The function is len(arr).

lens := len(t)
fmt.Println("Length:",lens)
Copy the code

If you declare an array with a length of… So the compiler will calculate the length of the corresponding array

a1 := [...]int{12.78.50}
fmt.Println(len(a1))
Copy the code
  • Gets the size of the array

The function is: cap(arr)

caps := cap(t)
fmt.Println("Capacity:",caps)
Copy the code
  • Through the array

The code above has shown an example of traversing the array through a for loop. There are other ways of traversing the array, using the range function.

a := [...]float64{67.7.89.8.21.78}
sum := float64(0)
for i, v := range a {//range returns both the index and val
    fmt.Printf("%d the element of a is %.2f\n", i, v)
    sum += v
}
fmt.Println("\nsum of all elements of a",sum)
Copy the code

If you only need the value and want to ignore the index, you can do so by replacing the index with the _ blank identifier.

for _, v := range a { 
  //ignores index  
}
Copy the code

2 d array

The Go language supports multidimensional arrays. The following are common syntax methods for declaring multidimensional arrays:

varvariable_name [SIZE1][SIZE2]... [SIZEN] variable_typeCopy the code
a = [3] [4]int{{0.1.2.3},/* The index of the first row is 0 */
 {4.5.6.7},/* The second row has an index of 1 */
 {8.9.10.11}   /* The index of the third row is 2 */
}
Copy the code

Multidimensional array usually used may be less, you can understand.

Features of arrays

Arrays are value types, and arrays in Go are value types, not reference types. This means that when they are assigned to a new variable, a copy of the original array is assigned to the new variable. If a change is made to the new variable, it is not reflected in the original array.

func main(a) {  
    a := [...]string{"1"."2"."3"."4"."5"}
    b := a // Assign a to B
    b[0] = "100"
    fmt.Println("a is ", a)
    fmt.Println("b is ", b) 
}
Copy the code

Running result:

a is [1 2 3 4 5]  
b is [100 2 3 4 5] 
Copy the code

The size of an array is part of the type. So [50]int and [25]int are different types. Therefore, arrays cannot be resized. Don’t worry about this limitation, because slicing exists to solve this problem.

package main

func main(a) {  
    a := [3]int{5.78.8}
    var b [5]int
    b = a // Cannot assign a value
}
Copy the code