SoFunction
Updated on 2025-03-03

Why does Go rarely use array principle analysis

Array introduction

In Go language, an array is a continuous piece of memory, and the array cannot be expanded. When passed as a parameter, it belongs to the value passing.

The length and type of the array jointly determine the type of the array. Different types of arrays cannot be compared, otherwise an error will be reported during compilation.

Because of some of the characteristics of arrays, we rarely use arrays in Go project development. In this article, we will introduce the characteristics of arrays.

Statement method

In Go language, there are three ways to declare arrays.

Sample code:

func main() {
    var arr1 [2]int
    var arr2 = [2]int{1, 2}
    var arr3 = [...]int{1, 2}
    (arr1)
    (arr2)
    (arr3)
}

Output result:

[0 0]
[1 2]
[1 2]

Read the above code, we declare arrays in three ways, among whicharr1andarr2The difference is,arr1The array was not assigned a value when declared, so the output result is a type zero value.[0 0]

It should be noted thatarr3The length of the array is not specified, but[...]Instead, this is actually the syntactic sugar that declares an array in Go language. When compiling, the length of the array is automatically inferred through the assignment of the array. We can use built-in functions.len()Query the length of the array.

Characteristics of arrays

After understanding the declaration method of arrays, let’s introduce what characteristics the array has.

The length and type of the array jointly determine the type of the array, for examplevar arr1 [2]intandvar arr2 [3]intIt is a different type. Moreover, different types of arrays cannot be compared, and arrays cannot be expanded.

If the length of the array is less than or equal to 4, the array will be optimized during compilation. The array will be initialized in the stack area when the program starts. We can also pay attention to this when using the array type.

When accessing elements in an array using array subscripts, an out-of-bounds access will be reported and an error will be reported during compilation. However, if we use variablesarr[i]As an array subscript, accessing elements in an array, it cannot be checked for out-of-bounds access during compilation, and will be raised at runtime.panic

Sample code:

func Store() {
    var arr [2]int
    for i := 0; i < 5; i++ {
        arr[i] = i + 1
    }
    (arr)
}

Output result:

panic: runtime error: index out of range [2] with length 2
goroutine 1 [running]:
...

When passing array-type variables as parameters, they are all value-passing. We should pay special attention when using array-type parameters.

Sample code:

func main() {
    var arr2 = [2]int{1, 2}
    Get(arr2)
    ("arr2=%p\n%d\n", &arr2, arr2)
}
func Get(arr [2]int) {
    ("Get()=%p\n%d\n", &arr, arr)
}

Output result:

Get()=0xc0000120f0
[1 2]
arr2=0xc0000120b0
[1 2]

Reading the above code, we can find that when the array is passed as a parameter, the address changes, and it can be proved that it belongs to the value passing, that is, allocating a new piece of memory and copying the value of the array into the new memory.

Summarize

In this article, we introduce some characteristics of arrays in the Go language to prove the reason why arrays are rarely used in Go project development.

There are two main reasons: one is that the array cannot be expanded; the other is value transfer. Be especially careful with large arrays. If you cannot avoid using large arrays, you can use array pointers.

The above is the detailed content of why Go rarely uses array principle analysis. For more information about the use of Go arrays, please pay attention to my other related articles!