Go language array


The Go language provides array type data structures.

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

Relative to declaring variables number0, number1, ..., and number99, using the array form numbers[0], numbers[1] ..., numbers[99] is more convenient and easy to expand.

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

arrays.jpg


Declaring arrays

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

var variable_name [SIZE] variable_type

The above is the definition of a one-dimensional array. Array length must be an integer and greater than 0. For example, the following defines the array balance with a length of 10 and a type of float32:

var balance [10] float32

Initializing the array

The following demonstrates array initialization:

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

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

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

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

This example is the same as the above example, although The size of the array is not set.

 balance[4] = 50.0

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

array_presentation.jpg


Accessing array elements

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

float32 salary = balance[9]

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

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

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 examples 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

More content

Arrays are very important to the Go language. Below we will introduce more content about arrays:

ContentDescription
Multidimensional arrayGo language supports multidimensional arrays. The simplest multidimensional array is a two-dimensional array
Pass array to functionYou can pass array parameters to function