SoFunction
Updated on 2025-03-04

Detailed explanation of the use of arrays for basic learning of Go language

Everyone must be familiar with the array image, and all major languages ​​also have arrays. Go also provides array-type data structures.

1. Array (array)

An array is a set of fixed-length elements of the same data type. In Go language, the length of the array cannot be changed after being declared. You can modify the elements of the array. Usage:

// eg: Define an int array of length 10var a [10]int

2. Declare an array

When declaring a Go language array, you need to specify the element type and number of elements. The syntax format is as follows:

var variable_name [SIZE] variable_type

3. Array initialization

There are many ways to initialize arrays in Go.

3.1 Method 1

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

balance := [5]float32{3.14, 13.0, 33.4, 15.0, 20.0}

3.2 Method 2

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

balance := [...]float32{3.14, 13.0, 33.4, 15.0, 20.0}

3.3 Method Three

If the length of the array is set, we can also initialize the element by specifying the subscript:

// Initialize the elements with index subscripts 1 and 3balance := [5]float32{1:2.0,3:7.0}

3.4 Multidimensional array

package main

import (
    "fmt"
)

var array1 [5][3]int
var array2 [2][3]int = [...][3]int{{10, 20, 30}, {4, 5, 6}}

func main() {
    arr1 := [2][3]int{{10, 11, 12}, {20, 21, 22}}
    arr2 := [...][2]int{{10, 11}, {20, 21}, {30, 31}}
    (array1, array2)
    (arr1, arr2)
}

Notice:In the multi-dimensional array of Go language, only the first layer can be used...Other dimensions cannot be used...

4. Iterate over the array & get the value

In Go, there are two ways to traverse arrays:

func main() {
    var users = [...]string{"Zhang San", "Li Si", "Wang Wu"}

    // Method 1: for range traversal    for index, item := range users {
        (index, item)
    }
    
    // Method 2: for loop traversal    for i := 0; i < len(users); i++ {
        ("Name:", users[i])
    }

}

Multidimensional array traversal

func main() {
    var f [2][3]int = [...][3]int{{10, 20, 30}, {4, 5, 6}}
    
    for k1, v1 := range f {
        for k2, v2 := range v1 {
            ("(%d,%d)=%d ", k1, k2, v2)
        }
        ("-------------")
    }
}

Notice:In Go language, access is out of bounds. If the subscript is outside the scope of the combination method, access is out of bounds, and it will appear.panic

5. Array copy and parameter transfer

In Go, arrays are value types, and assignments and transfers will copy the entire array. It will only change the value of the copied temporary array, not change itself.

package main

import "fmt"

func printArr1(arr *[5]int) {
    // Array pointer    arr[0] = 1000
}

func printArr2(arr [5]int) {
    arr[0] = 1000
}

func main() {
    arr1 := [5]int{100, 200, 300, 400, 500}
    printArr1(&arr1)
    (arr1)
    
    arr2 := [...]int{2, 4, 6, 8, 10}
    printArr2(arr2)
    (arr2)
}

Console output result:

[1000 200 300 400 500]
[2 4 6 8 10]

Notice:

  • Arrays support the "==","!=" operator because its memory is always initialized.
  • [n]T represents a pointer array, and [n]T represents an array pointer.

This is the end of this article about the detailed explanation of the use of arrays for basic learning of Go language. For more related contents of Go language arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!