Arrays, slices & maps


  • Fixed size, cannot be changed. From a syntax standpoint, this is what separates it from slices.
  • The size is part of the type definition. [5]int != [4]int
  • An array is a value, passing it around means copying the value (unlike C where an array is a pointer to the first element in memory). Can create pointers to arrays and pass it around for the same effect.
  a := [5]int
  b := [10]SomeStruct
  c :=[...]int{1,2,3}  // but this is a slice --> c:=[]int{12,13,14,15}


  • Slices are references to arrays, built on top of array
  • Size is not fixed and can be increased using the append built-in function
  • Syntatically the difference between a slice and array is omitting the size or ... in the definition


  • You can create a new slice by “slicing” an existing slice or array
  • An important point to remember is when “sliced” the new slice shares the underlying storage
first := []byte{'g','o','l','a','n','g'}
second := first[1:3]
fmt.Printf("%s\n",first) // golang
fmt.Printf("%s\n",second) // ol

fmt.Printf("%s\n",first) // goXang
fmt.Printf("%s\n",second) // oX



  • Valid keys - comparable types: boolean, numeric, string, pointer, channel, and interface types, and structs or arrays that contain only those types


var m map[string]int
  • Map types are reference types, like pointers or slices, and so the value of m above is nil; it doesn’t point to an initialized map

  • Initialise a nil map with make m := make(map[string]int)

Getting and setting

  • a := m[“doesntexist”] - a will be assigned the “zero value” of that type
  • a, ok := m[“doesntexist”] - ok will be assigned false
  • , ok := m[“doesntexist”]- same thing as above
  • m[“exists”] = 1


  • delete(m, “exists”)


  • for k, v := range m {}



  • Should you create an array of pointers. For example, which is better?
  sayans := []*Sayan{
    &Sayan{Name: "Goku", Power: 9001,}