P4 - ArraysSlicesMaps

Arrays

var x [5]int

OR

x := [5]float64{ 98, 93, 77, 82, 83 }

OR

x := [5]float64{

98,

93,

77,

82,

83, //Note - even the last element ended by a comma (,)

}

x is an example of an array which is composed of 5 ints.

Length of array = len(<array_name>)

Index, Value = range <array_name>

package main

import "fmt"

func main() {

var x [5]int

x[4] = 100

fmt.Println(x)

}

You should see this:

[0 0 0 0 100]

Major issue with arrays

Their length is fixed. In order to add/remove an item, we actually had to change the size as well. Go's solution to this problem is to use a different type: slices

Slices

  • A slice is a segment of an array. Like arrays slices are indexable and have a length.

  • Unlike arrays this length is allowed to change.

Here's an example of a slice: var x []float64

  • The only difference between this and an array is the missing length between the brackets. In this case x has been created with a length of 0.

  • If you want to create a slice you should use the built-in make function:

x := make([]float64, 5)

  • This creates a slice that is associated with an underlying float64 array of length 5.

  • Slices are always associated with some array, and

  • although they can never be longer than the array, they can be smaller.

  • The make function also allows a 3rd parameter:

x := make([]float64, 5, 10)

10 represents the capacity of the underlying array which the slice points to.

  • Another way to create slices is to use the [low : high] expression:

arr := []float64{1,2,3,4,5}

x := arr[0:5]

  • low is the index of where to start the slice and high is the index where to end it (but not including the high index itself).

For example while

arr[0:5] returns [1,2,3,4,5], -->Index 5 is not included

arr[1:4] returns [2,3,4]. -->Index 4 is not included

  • For convenience we are also allowed to omit low, high or even both low and high.

arr[0:] is the same as arr[0:len(arr)],

arr[:5] is the same as arr[0:5] and

arr[:] is the same as arr[0:len(arr)].

Slice Functions

Go includes two built-in functions to assist with slices: append and copy. Here is an example of append:

func main() {

slice1 := []

int{1,2,3}

slice2 := append(slice1, 4, 5)

fmt.Println(slice1, slice2)

}

After running this program slice1 has [1,2,3] and slice2 has [1,2,3,4,5]. append creates a new slice by taking an existing slice (the first argument) and appending all the following arguments to it.

Here is an example of copy:

func main() {

slice1 := []

int{1,2,3}

slice2 := make([]int, 2)

copy(slice2, slice1)

fmt.Println(slice1, slice2)

}

After running this program slice1 has [1,2,3] and slice2 has [1,2]. The contents of slice1 are copied into slice2, but since slice2 has room for only two elements only the first two elements of slice1 are copied.

Maps

A map is an un-ordered collection of key-value pairs. Also known as an associative array, a hash table or a dictionary, maps are used to look up a value by its associated key. Here's an example of a map in Go:

var x map[ <Key_Type> ] <Value_Type>

var x map[string]int