Go Arrays: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:
=Overview=
=Overview=


An array is a numbered sequence of elements, ''of a single type'', and ''with a fixed length''.
An array is a numbered sequence of elements, ''of a single type'', and ''with a fixed length''. The length of the array is part of the type name, arrays with different lengths are different types, even if the element type is the same.


=Declaration=
=Declaration=

Revision as of 23:05, 27 March 2016

Internal

Overview

An array is a numbered sequence of elements, of a single type, and with a fixed length. The length of the array is part of the type name, arrays with different lengths are different types, even if the element type is the same.

Declaration

var a [5]int

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

Type inference and initialization declaration:

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

Array Literals

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

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 that position.

Array Length

len()