Easy Tutorial
❮ Go Constants Go Pointer To Pointer ❯

Go Language Arrays

Go language provides an array type data structure.

An array is a sequence of data items of the same unique type, which are numbered and have a fixed length. This type can be any primitive type such as integers, strings, or custom types.

Instead of declaring individual variables like number0, number1, ..., number99, using the array format numbers[0], numbers[1], ..., numbers[99] is more convenient and easier to extend.

Array elements can be accessed (or modified) through their index (position), which starts at 0. The first element is indexed at 0, the second at 1, and so on.


Declaring Arrays

To declare an array in Go, you need to specify the element type and the number of elements. The syntax is as follows:

var variable_name [SIZE] variable_type

This is the definition for a one-dimensional array. For example, the following defines an array named balance with a length of 10 and type float32:

var balance [10] float32

Initializing Arrays

The following demonstrates array initialization:

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

We can also quickly initialize an array during its declaration using literals:

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

If the array length is uncertain, you can use ... instead of the array length, and the compiler will determine the length 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 array length is set, you can initialize elements by specifying their indices:

// Initialize elements at indices 1 and 3
balance := [5]float32{1:2.0, 3:7.0}

The number of elements in {} cannot exceed the number in [] during array initialization.

If the size in [] is omitted, Go will set the array size based on the number of elements:

balance[4] = 50.0

This example accesses the fifth element. Array elements can be accessed (or modified) through their index, starting from 0.


Accessing Array Elements

Array elements can be accessed using their index. The format is the array name followed by brackets containing the index value. For example:

var salary float32 = balance[9]

This example accesses the 10th element of the array balance.

The following demonstrates a complete array operation (declaration, assignment, access):

Example 1

package main

import "fmt"

func main() {
   var n [10]int /* n is an array of 10 integers */
   var i,j int

   /* Initialize array n with elements */
   for i = 0; i < 10; i++ {
      n[i] = i + 100 /* Set element to i + 100 */
   }

   /* Print the value of each array element */
   for j = 0; j < 10; j++ {
      fmt.Printf("Element[%d] = %d\n", j, n[j] )
   }
}

The output of the above example is:

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
   // Declare and quickly initialize an array
   balance := [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0}

   /* Print array elements */
   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}
   /* Print the value of each array element */
   for j = 0; j < 5; j++ {
      fmt.Printf("balance2[%d] = %f\n", j, balance2[j] )
   }
}
fmt.Printf("balance2[%d] = %f\n", j, balance2[j])
}

// Initialize elements at indices 1 and 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 above example produces the following output:

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

More Content

Arrays are very important in Go, and here we introduce more about arrays:

Content Description
Multidimensional Arrays Go supports multidimensional arrays, the simplest of which is a two-dimensional array
Passing Arrays to Functions You can pass array parameters to functions
❮ Go Constants Go Pointer To Pointer ❯