Go Arrays: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 71: Line 71:
==Indexing Operator==
==Indexing Operator==


Indexing operator <tt>[[Go Concepts - Operators#.5B.5D|[]]]</tt> returns 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:
Indexing operator <tt>[[Go Concepts - Operators#.5B.5D|[]]]</tt> 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:
Compile-time error:

Revision as of 05:20, 6 April 2016

Internal

Overview

An array is fixed-length data type that contains a contiguous and numbered sequence of elements, of a single type, and with a fixed length. The type can be a Go built-in type or an user type. The length of the array is part of the type name, arrays with different lengths are of different types, even if the element type is the same.

Once an array is declared, its type and length cannot be changed.

Arrays are rarely directly used in Go code, slices are used instead.

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.

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.