Declaration and use of array in Go language

One, array definition

    1.1 The data structure of array is provided in Go language . Array is a fixed-length data sequence with the same type. This type can be any basic data type or compound data type and custom Go built-in containers-arrays and cut types.
    1.2 Array elements can read or modify element data through index subscript (position). The index starts from 0, the index of the first element is 0, the index of the second is 1, and so on. The subscript value range of the array is from 0 to the length minus 1.
    1.3 Once the array is defined, its size cannot be changed.

Second, the syntax of the array

2.1 Declare an array

    The Go language array declaration needs to specify the element type and the number of elements . The syntax format is as follows:

  • var variable name [array length] data type
  • The above is the definition of a one-dimensional array
  • Array length must be an integer and greater than 0
  • Uninitialized array is not nil, which means that there is no empty array (unlike cut)

2.2 Initialize the array

  • var nums = [5]int{1, 2, 3, 4 ,5 }, the number of elements in {} in the initialization array cannot be larger than the number in [].
  • If you ignore the number in [] and don't set the size of the array, the Go language will set the size of the array according to the number of elements.
    1) You can ignore the length of the array in the declaration and replace it with..., the compiler will automatically calculate the length;
    2) var nums = […]int{1, 2, 3, 4 ,5 };
    3) This The example is the same as the example above, although the size of the array is not set;
  • For example, var myNum int = nums[2]
    means that myNum is the third element in the read array nums[]. Array elements can be read (or modified) by index (position), the index starts from 0, the index of the first element is 0, the index of the second is 1, and so on.

Three, the length of the array

    By passing the array as a parameter to the len() function, the length of the array can be obtained.
    If you ignore the length of the array in the declaration and replace it with..., the compiler can find the length.

Fourth, traverse the array

//myArray.go

package main

import (
	"fmt"
)

func main() {
    
    
	//fmt.Println("Hello World!")
	a := [4]float64{
    
    67.7, 89.8, 21, 78}
	b := [...]int{
    
    2, 3, 5}

	//方法一,遍历数组
	for i := 0; i < len(a); i++ {
    
    
		fmt.Print(a[i], "\t")
	}

	fmt.Println()
	//方法二,遍历数组
	for _, value := range b {
    
    
		fmt.Print(value, "\t")
	}
}

Five, multi-dimensional array

5.1 Go language supports multi-dimensional arrays,

    The following are commonly used multi-dimensional array declaration methods:

  • var variable_name [SIZE1][SIZE2]…[SIZEn] variable_type
  • The following example declares a three-dimensional integer array: var threedim [5][10][4]int

5.2 Two-dimensional array

  • A two-dimensional array is the simplest multidimensional array, and a two-dimensional array is essentially composed of a one-dimensional array. The definition of a two-dimensional array is as follows:
  • var arrayName [ x ][ y ] variable_type
  • For example: a = [3][4]int{ {0, 1, 2, 3}, /* The first index is 0 / {4, 5, 6, 7}, / The second index is 1 / { 8, 9, 10, 11}, /The third index is 2 */ }



5.3 Accessing a two-dimensional array

  • A two-dimensional array is accessed by specifying coordinates, such as the row index and column index in the array.
  • For example: var myValue int = a[2][3]
  • That is, myValue represents the fourth element of the third row of the two-dimensional array a.

5.4 Two-dimensional array traversal

    Two-dimensional arrays can be traversed using 2-level loops.
//myArray2.go

package main

import (
	"fmt"
)

func main() {
    
    
	var a = [5][2]int{
    
    {
    
    0, 0}, {
    
    1, 2}, {
    
    2, 4}, {
    
    3, 6}, {
    
    4, 8}}
	fmt.Println(len(a))
	fmt.Println(len(a[0]))

	for i := 0; i < len(a); i++ {
    
    
		for j := 0; j < len(a[0]); j++ {
    
    
			fmt.Printf("a[%d][%d]= %d\n", i, j, a[i][j])
		}
	}
}

Six, numbers are value types

    6.1 Arrays in Go language are value types, not reference types. This means that when they are assigned to a new variable, a copy of the original array will be assigned to the new variable. If the new variable is changed, it will not be reflected in the original array.

    6.2 When passing arrays to functions as parameters, they will be passed by value, but the original array will remain unchanged.

    6.3 Case myArray3.go
//myArray3.go

package main

import (
	"fmt"
)

func main() {
    
    
	//3)数组传递,是值传递,不是引用传递
	a := [...]string{
    
    "USA", "China", "India", "Germany", "France"}
	b := a
	b[0] = "Singapore"
	fmt.Println("a:", a)
	fmt.Println("b:", b)
}

    The effect is as follows:

Figure (1) Array is value transfer

Guess you like

Origin blog.csdn.net/sanqima/article/details/108894778