Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exercise: count words

  • Count how many times each word appears in a given slice
  • You can use this skeleton:
package main

import (
	"fmt"
)

func main() {
	celestialObjects := []string{"Moon", "Gas", "Asteroid", "Dwarf", "Asteroid", "Moon", "Asteroid"}
	fmt.Println(celestialObjects)

}
  • Expected output:
[Moon Gas Asteroid Dwarf Asteroid Moon Asteroid]
Moon       2
Gas        1
Asteroid   3
Dwarf      1