[Go] Go language tutorial--GO language array (11)

Past review:


Go language provides a data structure of array type.

An array is a numbered, fixed-length sequence of data items of the same unique type, which can be any primitive type such as integer, string, or a custom type.

Compared with declaring variables of number0, number1, …, number99, it is more convenient and easy to expand to use the array form numbers[0], numbers[1] …, numbers[99].

Array elements can be read (or modified) by index (position), the index starts from 0, the first element index is 0, the second index is 1, and so on.

insert image description here

declare array

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

var variable_name [SIZE] variable_type
above is the definition of one-dimensional array. For example, the following defines the array balance with a length of 10 and a type of float32:

var balance [10] float32

Initialize the array

The following demonstrates array initialization:

var balance = [5]float32{
    
    1000.0, 2.0, 3.4, 7.0, 50.0}

We can also quickly initialize the array while declaring the array through the literal:

balance := [5]float32{
    
    1000.0, 2.0, 3.4, 7.0, 50.0}

If the length of the array is uncertain, you can use ... instead of the length of the array, and the compiler will infer the length of the array by itself based on the number of elements:

var balance = [...]float32{
    
    1000.0, 2.0, 3.4, 7.0, 50.0}

or

balance := [...]float32{
    
    1000.0, 2.0, 3.4, 7.0, 50.0}

If the length of the array is set, we can also initialize elements by specifying subscripts:

//  将索引为 1 和 3 的元素初始化
balance := [5]float32{
    
    1:2.0,3:7.0}

The number of elements in {} in the initialization array cannot be greater than the number in [].

If the number in [] is ignored and the size of the array is not set, the Go language will set the size of the array according to the number of elements:

 balance[4] = 50.0

The above example reads the fifth element. Array elements can be read (or modified) by index (position), the index starts from 0, the first element index is 0, the second index is 1, and so on.

insert image description here

access array elements

Array elements can be read by index (position). The format is to add square brackets after the array name, and the value of the index is in the square brackets. For example:

var salary float32 = balance[9]

The above example reads the value of the 10th element of the array balance.

The following demonstrates an example of the complete operation (declaration, assignment, access) of an array:

Example 1
package main

import "fmt"

func main() {
    
    
   var n [10]int /* n 是一个长度为 10 的数组 */
   var i,j int

   /* 为数组 n 初始化元素 */        
   for i = 0; i < 10; i++ {
    
    
      n[i] = i + 100 /* 设置元素为 i + 100 */
   }

   /* 输出每个数组元素的值 */
   for j = 0; j < 10; j++ {
    
    
      fmt.Printf("Element[%d] = %d\n", j, n[j] )
   }
}

The execution results of the above example are as follows:

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

Example 2
package main

import "fmt"

func main() {
    
    
   var i,j,k int
   // 声明数组的同时快速初始化数组
   balance := [5]float32{
    
    1000.0, 2.0, 3.4, 7.0, 50.0}

   /* 输出数组元素 */         ...
   for i = 0; i < 5; i++ {
    
    
      fmt.Printf("balance[%d] = %f\n", i, balance[i] )
   }
   
   balance2 := [...]float32{
    
    1000.0, 2.0, 3.4, 7.0, 50.0}
   /* 输出每个数组元素的值 */
   for j = 0; j < 5; j++ {
    
    
      fmt.Printf("balance2[%d] = %f\n", j, balance2[j] )
   }

   //  将索引为 1 和 3 的元素初始化
   balance3 := [5]float32{
    
    1:2.0,3:7.0}  
   for k = 0; k < 5; k++ {
    
    
      fmt.Printf("balance3[%d] = %f\n", k, balance3[k] )
   }
}

The execution results of the above example are as follows:

balance[0] = 1000.000000
balance[1] = 2.000000
balance[2] = 3.400000
balance[3] = 7.000000
balance[4] = 50.000000
balance2[0] = 1000.000000
balance2[1] = 2.000000
balance2[2] = 3.400000
balance2[3] = 7.000000
balance2[4] = 50.000000
balance3[0] = 0.000000
balance3[1] = 2.000000
balance3[2] = 0.000000
balance3[3] = 7.000000
balance3[4] = 0.000000

Guess you like

Origin blog.csdn.net/u011397981/article/details/131649896