Intro to Go arrays¶
An array is a numbered sequence of elements of a specific length. That is, the type of the array encodes the type of the array constituents and the length. In other words, the array’s length is part of the type of the array (like tuples Haskell or TypeScript).
Declare an array of two int
elements:
var xs [2]int
fmt.Println(len(xs))
//=> 2
fmt.Println(xs)
//=> [0 0]
xs
above was not initialized to any values, so it is zero-valued, which for int
means 0
s.
Note the array is printed inside a pair of square brackets.
And then we can assign values by using indexes:
var xs [2]int
fmt.Println(len(xs))
//=> 2
xs[0] = 10
xs[1] = 20
fmt.Println(xs)
//=> [10, 20]
And it is an error trying to assign or read out bounds of the array indexes:
var xs [2]int
xs[3] = 1
// Error:
// invalid argument: index 3 out of bounds [0:2]
fmt.Println(xs[5])
// Error:
// invalid argument: index 5 out of bounds [0:2]
It is possible to let the compiler count the number of elements when declaring and initializing:
var xs = [...]int{10, 20}
var ys = [...]int{10, 20, 30}
And even if we type the array variables on the left side of the assignment operator, it is still required to type the “braces composite literal” initialization on the right side too!
var xs [2]int = [...]int{10, 20}
var ys [3]int = [...]int{10, 20, 30}
// Same as:
var xs [2]int = [2]int{10, 20}
var ys [3]int = [3]int{10, 20, 30}
And make sure the number of elements on the left (inside []
) matches the actual number of elements on the right.
The index: syntax¶
If specifics indexes are assigned values with the idx:
syntax, elements in between are zeroed:
var xs [5]int = [...]int{100, 3: 400, 500}
fmt.Println(xs)
//=> [100 0 0 400 500]
But after the idx:
syntax, we have to fill in the rest to match the length or we’ll get an error:
var xs [5]int = [...]int{100, 3: 400 }
fmt.Println(xs)
// Error: cannot use [...]int{…} (value of type [4]int) as [5]int
// value in variable declaration
Here we have 100 for index 0, index 1 and 2 get zeroed, and we explicitly use the idx:
syntax to assign the value 400 to index 3.
But the compiler does NOT apply the zero-value to index 4 and thus we get an error.
When using the idx:
syntax, we must make sure all the remaining positions (if any) are correctly provided the expected values.
Only values in between get zero-valued.
Multi-dimensional¶
By composing types, it is possible to nest arrays, thus achieving multi-dimensional arrays 😎!
var matrix [2][3]int = [2][3]int{
{10, 20, 30},
{40, 50, 60},
}
fmt.Println(matrix)
//=> [[10 20 30] [40 50 60]]
Of courses the sizes of the arrays types on the left and right hand side of the assignment operator must match. Still, the left hand side can be omitted and the compiler infers it from the right hand side type:
var matrix = [2][3]int{
{10, 20, 30},
{40, 50, 60},
}
fmt.Println(matrix)
//=> [[10 20 30] [40 50 60]]
While the syntax to create the nested arrays uses curly braces {{}, {}}
, it prints in square brackets.
And while we can use the […]
syntax for one array, we cannot use that syntax to create multi-dimensional arrays:
var xs [2]int = [...]int{1, 2}
// OK
var ys [2][3]int = [...][...]int{{1, 2, 3}, {4, 5, 6}}
// Error: invalid use of [...] array (outside a composite literal).
TODO: Research more on the error and the syntax.
Some say […][…]int
is not valid syntax for a type.
.hljs-comment, pre.pygments .tok-c1 { font-style: normal; }