rmoff's random ramblings
about talks

Learning Golang (some rough notes) - S01E03 - Maps

Published Jun 29, 2020 by in Go, Golang, Maps at https://preview.rmoff.net/2020/06/29/learning-golang-some-rough-notes-s01e03-maps/

👉 A Tour of Go : Exercise - Maps

Implement WordCount

This is probably bread-and-butter for any seasoned programmer, but I enjoyed the simple process and satisfaction of breaking the problem down into steps to solve using what the tutorial had just covered. Sketching out the logic in pseudo-code first, I figured that I wanted to do this:

  • For each word in the phrase:

    • Check if the word exists in the map already

      • Create it if it doesn’t

    • Add one to the map value

Note
Learning Go : Background

Using Printf it was useful to check on how it was executing.

func WordCount(s string) map[string]int {
	w := make(map[string]int)

	for i, v := range strings.Fields(s) {
		fmt.Printf("Index: %d value %v\n",i, v)
		if _, o := w[v]; o == true {
			fmt.Printf("\tExisting map found for %v with value %d\n",v,w[v])
			w[v] = w[v] + 1
		} else {
			fmt.Printf("\tCreating new map for %v with value 1\n",v)
			w[v] = 1
		}
	}

	return w
}

I liked that the tutorial uses tests to check what you’ve done, and shows the expected output:

PASS
 f("I am learning Go!") = 
  map[string]int{"Go!":1, "I":1, "am":1, "learning":1}
PASS
 f("The quick brown fox jumped over the lazy dog.") = 
  map[string]int{"The":1, "brown":1, "dog.":1, "fox":1, "jumped":1, "lazy":1, "over":1, "quick":1, "the":1}
PASS
 f("I ate a donut. Then I ate another donut.") = 
  map[string]int{"I":2, "Then":1, "a":1, "another":1, "ate":2, "donut.":2}
PASS
 f("A man a plan a canal panama.") = 
  map[string]int{"A":1, "a":2, "canal":1, "man":1, "panama.":1, "plan":1}

Looking at the pseudo-code and what Golang will handle for you automagically it can be reduced to this:

  • For each word in the phrase:

    • Check if the word exists in the map already

      • Create it if it doesn’t

    • Add one to the map value (implicitly create the map entry if it doesn’t already exist)

func WordCount(s string) map[string]int {
	w := make(map[string]int)

	for _, v := range strings.Fields(s) {
		w[v] = w[v] + 1
	}

	return w
}
Note
the underscore character, representing a required variable that you’re not going to use, is pretty useful.

📺 More Episodes… đź”—

  • Kafka and Go

    • S02E00 - Kafka and Go

    • S02E01 - My First Kafka Go Producer

    • S02E02 - Adding error handling to the Producer

    • S02E03 - Kafka Go Consumer (Channel-based)

    • S02E04 - Kafka Go Consumer (Function-based)

    • S02E05 - Kafka Go AdminClient

    • S02E06 - Putting the Producer in a function and handling errors in a Go routine

    • S02E07 - Splitting Go code into separate source files and building a binary executable

    • S02E08 - Checking Kafka advertised.listeners with Go

    • S02E09 - Processing chunked responses before EOF is reached

  • Learning Go

    • S01E00 - Background

    • S01E01 - Pointers

    • S01E02 - Slices

    • S01E03 - Maps

    • S01E04 - Function Closures

    • S01E05 - Interfaces

    • S01E06 - Errors

    • S01E07 - Readers

    • S01E08 - Images

    • S01E09 - Concurrency (Channels, Goroutines)

    • S01E10 - Concurrency (Web Crawler)


Robin Moffatt

Robin Moffatt is a Principal DevEx Engineer at Decodable. He likes writing about himself in the third person, eating good breakfasts, and drinking good beer.

Story logo

© 2024