Go Arrays

From NovaOrdis Knowledge Base
Revision as of 16:16, 24 August 2023 by Ovidiu (talk | contribs) (→‎Overview)
Jump to navigation Jump to search

External

Internal

TO DISTRIBUTE

Internal

Implementation Details

Arrays are fast: data is maintain in continuous blocks of memory, which keeps them in the CPU caches longer. Index access and arithmetic is fast: the type information and the guarantee that all elements are of same type allows calculating "distance in memory" to find elements.

Arrays are Values

An array is a value in Go. It can be used in an assignment operation. The variable name denotes the entire array, thus an array can be assigned to other arrays of the same type (length and element type). When an array2 is assigned to an array1, the values of array2 are copied across to array1.

In Go, function arguments are passed by value. The fact that arrays are values also has implications on how arrays are passed between functions: by value. A copy of the entire array is made and placed on the stack, regardless of the array's size. For large arrays, this can have performance implications. That is why, passing pointers to arrays is preferred. Even better, use slices.

Declaration

Long Declaration

var a [5]int

A declaration without explicit initialization initializes the array with the type's zero value.

Long declaration with explicit initialization with an array literal:

var b [3]int = [3]int{1, 2, 3}

Go provides syntax for initializing just some of the elements with specified values, letting the compiler to initialize the rest with zero values. The syntax is:

var c [20]int = [100]int{0:10, 1:20}

In the example above, the element 0 is initialized to 10, the element 1 is initialized to 20 and the rest are initialized to their zero value (0).

Short Declaration

The short declaration:

a := [3]int{1, 2, 3}

The short declaration is invalid for arrays if the length and type is not followed by the initialization literal { ... }, the initialization literal is required.

For the format above, there's redundancy in the requirement to specify the length of the array between square brackets and the length of the literal. The compiler can infer the length of the array from the literal, and the declaration is:

a := [...]int{1, 2, 3}

The literal can be specified "vertically":

a := [...]int{
        1, 
        2, 
        3, 
   }

The extra trailing comma is required when the elements are specified on separate lines, but not when they're specified in a single line. This is to allow commenting out elements without breaking the program.

Array Operators and Functions

Indexing Operator

Indexing operator [] returns the value at the given position and can also be used to change the value at that position. Accesses are bound-checked and an attempt to access an element out of bounds produces a compile-time or runtime error:

Compile-time error:

./main.go:13: invalid array index 3 (out of bounds for 3-element array)

Runtime error:

panic: runtime error: index out of range

Array Length

len() returns the length of the array.

Iterating over Arrays

range Keyword

Multidimensional Arrays

TODO Go in Action page 84.