Array

Array

Arrays hold multiple elements of the same type, but the length is fixed.

  • the length is part of the array's type and therefore cannot be changed.
// create an array of integers with length 5
var a [5]int

// create an array with initialized values
var a [5]int{5, 4, 3, 2, 1}

Slice

Slices are an abstraction over arrays to make them easier to work with, most notably because they do not have a fixed length.

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

In particular, a slice is a reference to a segment of an array.

With slices we can do things like append (which doesn't modify the original Slice, but returns a new one)

  • under the hood, Go is creating a new array, copying the contents from the old one, and adding the new value.


Children
  1. Slice