Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Go language array


May 11, 2021 Go


Table of contents


Go language array

The Go language provides a data structure for array types.

An array is a series of numbered and fixed-length data items with the same unique type, which can be any original type, such as a shaping, string, or custom type.

Compared to declaring the variables of number0, number1, ..., and number99, it is more convenient and easy to expand using the array form numbers.

Array elements can be read (or modified) by indexes (locations), starting at 0, with the first element index at 0, the second index at 1, and so on.

Go language array


Declare the array

The Go language array declaration needs to specify the type of element and the number of elements, in the following syntax format:

var variable_name [SIZE] variable_type

The above is how one-dimensional arrays are defined. T he array length must be integer and greater than 0. For example, the following defines an array balance length of 10 type 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}

The number of elements in the initialization array cannot be greater than the number in .

If you ignore that the numbers in , do not set the size of the array, the Go language sets the size of the array based on the number of elements:

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

The instance is the same as the one above, 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 indexes (locations), starting at 0, with the first element index at 0, the second index at 1, and so on.

Go language array


Access array elements

Array elements can be read by index (position). T he format is the number of arrays with parentheses, which are the values of the index. 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 an array's full operation (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 above examples perform 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

Arrays are very important for the Go language, and we'll cover more of them below:

Content Describe
Multi-dimensional array The Go language supports multi-dimensional arrays, and the simplest multi-dimensional arrays are two-dimensional arrays
Pass an array to the function You can pass array arguments like a function