The “Maps” exercise in A Tour of Go asks you to implement WordCount, a function that returns a map[string]int counting each word in an input string, and the usual working solution splits the string on whitespace with strings.Fields and increments the map entry for each field. The exercise page itself supplies the surrounding main and imports; your job is the function body, and the classic answer is a few lines using strings.Fields plus wc[word]++.
What the exercise actually asks for
A Tour of Go’s maps lesson ends with an exercise whose description is essentially: given a string, count how many times each word appears, and return the counts as a map[string]int. The page provides a WordCount stub and a main that calls it on a fixed test string and prints the map. The expected output shape is a Go map literal like map[a:1 cat:2 ...]; exact ordering is not part of the contract because Go map iteration order is deliberately unspecified.
The important ambiguity is what counts as a “word.” The exercise statement is deliberately loose. The canonical solution treats any run of non-space characters as a word and splits on whitespace. That means punctuation stays attached: in a string containing "the cat's", you get cat's as a key, not cat and s. If your test string contains commas or periods, those characters remain part of the keys. The exercise does not require stripping punctuation, lowercasing, or handling Unicode word boundaries. If you do those things, you have written a stricter tokenizer, which is fine as an extension but is not what the grader on the Tour page checks.
A second ambiguity is the input string. The Tour’s exercise text uses a test string with repeated words; the precise string is part of the page, and it is the thing your function is run against when you press Run. You do not need to reproduce it from memory — read it off the page — but the logic must be general enough to handle any whitespace-separated input.
The canonical solution and why each piece is there
strings.Fields(s) splits on any run of Unicode whitespace and drops empty strings, so multiple spaces, tabs, and newlines do not produce empty keys. That is the property that makes it the right tool here: strings.Split(s, " ") would produce empty strings for double spaces and would not handle tabs at all.
The zero value of a map’s element type is what makes the increment idempotent. For a map[string]int, reading a missing key returns 0, so wc[word]++ is equivalent to “read current count (zero if absent), add one, store.” This is the idiom the exercise is teaching. It works because int’s zero value is 0; it would not work the same way for a map whose value type is a struct with no useful zero value, and it does not work for maps of slices, because wc[word] on a missing key yields a nil slice and append to it would not write back to the map without an explicit assignment.
make(map[string]int) versus a composite literal map[string]int{} makes no functional difference here; both create an empty, non-nil map ready for writes. Writing to a nil map panics, so the make (or literal) is required — you cannot just declare var wc map[string]int and start assigning.
Common mistakes and what each one reveals
Forgetting to initialize the map. var wc map[string]int; wc[word]++ compiles and then panics at runtime with “assignment to entry in nil map.” The Tour environment will show this as a runtime panic, not a compile error, which can be confusing if you are skimming.
Using strings.Split instead of strings.Fields. With a test string that has single spaces only, this appears to work. With doubled spaces it produces an empty-string key with a nonzero count, and it misses tab-separated words entirely. If your output includes something like :1, this is why.
Returning the map by printing inside the function. The signature returns map[string]int; the Tour’s main does the printing. Printing inside WordCount and returning nil will produce output but will not match the expected return-value check if the page uses one, and it is the wrong shape for the exercise regardless.
Sorting the output. Map iteration order is randomized per run in Go, so any solution that relies on a particular order of printed keys is fragile. The check on the Tour page compares map contents, not printed order, but if you write your own main for practice, sort keys explicitly if you want stable output.
Trying to mutate a map value through a range variable. This is not a mistake specific to this exercise, but it comes up: for _, v := range m { v++ } does not change the map, because v is a copy. You must write back with m[k] = v or, for counters, index directly.
What the exercise is really teaching
The maps lesson sits after slices and before the closure-based Fibonacci exercise. The pedagogical point is not tokenization — it is the map zero value and the m[k]++ idiom, plus the basic mechanics of make, assignment, and lookup. That is why the exercise statement does not specify punctuation handling: the intended solution is deliberately minimal, and adding a regex tokenizer would obscure the lesson.
It also reinforces that map keys are compared by equality, so strings are fine keys. It does not teach concurrency safety: a plain map[string]int is not safe for concurrent writes, and if you were counting words across goroutines you would need a mutex or a sync.Map (or per-goroutine maps merged at the end). The exercise is single-threaded, so this does not arise, but it is the natural next question and the answer is “not with a bare map.”
Extensions that go beyond the exercise
If you want to make WordCount behave like a “real” word counter, the usual additions are lowercasing (strings.ToLower) and stripping punctuation. Stripping punctuation properly is harder than it looks: a naive strings.TrimFunc with unicode.IsPunct removes apostrophes from contractions and hyphens from compound words, and it does not handle Unicode punctuation classes consistently with what a reader would consider a word boundary. A regex like [A-Za-z0-9']+ handles ASCII but drops accented letters; unicode.IsLetter plus unicode.IsDigit handles more but still mishandles scripts without spaces. None of this is required, and any of it changes the expected output for the Tour’s test string, so do it in a separate function or a copy of the exercise rather than in the submitted body if you are checking against the page.
A second natural extension is deterministic output: collect keys into a slice, sort.Strings it, and print in that order. That is the standard way to make map-derived output reproducible, and it is worth knowing because “why does my output change every run” is the most common confusion people have with map-printing exercises. The answer is that Go randomizes map iteration order on purpose, to prevent code from depending on it.
A third is counting words from a file or stdin rather than a literal string, which is the shape of the classic “word frequency” program. That is outside the exercise but uses the same core: scan tokens, counts[token]++, then sort for display.
How to check your work without the Tour
Run the function against a few hand-checkable inputs. "a b a" should give a:2, b:1. "a b" (two spaces) should give a:1, b:1 and no empty key if you used Fields. "" should give an empty map. "one" should give one:1. If any of those fail, the bug is almost certainly nil-map initialization, Split versus Fields, or an early return.
The Tour page itself runs your code in a sandbox and shows the printed result; if the printed map matches the expected counts for the page’s test string, you have solved it. Remember that the printed order may differ between runs and that is not a failure.
FAQ
Do I need to import anything besides strings? For the canonical solution, yes, only strings (the Tour page already imports what its main needs, and you add strings if it is not there). You do not need fmt inside WordCount unless you are debugging.
Does the exercise require handling punctuation or case? No. The canonical solution does neither, and adding either changes the keys for the Tour’s test string.
Why does my map print in a different order each time? Go deliberately randomizes map iteration order. Sort the keys if you need stable output.
Can I use a map[string]*int or a struct value? You can, but it is strictly more work for this exercise: pointers need allocation before increment, and struct values need read-modify-write. The plain map[string]int with wc[word]++ is the intended and simplest correct form.
Is strings.Fields Unicode-aware? It splits on Unicode whitespace as defined by unicode.IsSpace, so it handles non-breaking spaces and other Unicode space characters, not just ASCII space and tab. That is another reason to prefer it over Split(s, " ").