Chapter 10

Arrays in Go has a fixed size.

Note that arrays are immutable.

Immutable means that the object created cannot be changed or modified after its creation.

We can obtain the length of an array using the len function. For example,len(a) returns the length of an array a.

We can address till the length of the array, but tryig to access beyond this length, will return an "arrays out of bounds" error.

We can also append to an array, i.e. add new elements. However, since arrays are immutable, appending to an array will create a new instance. This means that the entire original array is copied and a new element is added to it.

Arrays have contiguous memory which makes accessing elements faster.

Defining an array: var a [4]int. This creates an array of length 4, initialized to all zeroes.

// both b and c are equivalent
b := [15]int{1, 2, 3, 4, 5}
c := [...]int{1, 2, 3, 4, 5}
// %v means any value
fmt.Printf("%v", b)
// Output
[1 2 3 4 5 0 0 0 0 0 0 0 0 0 0]

Difference from C#

Arrays are values. Unlike C, arrays are not pointers to the first array element. When we pass around an array value, we are passing by value (we make a copy). We could alternatively pass a pointer to the array.

We can think of array as a sort of struct with indexes.