为什么我在Golang巡回演出#43中的文字计数器不起作用?

I'm trying to resolve the puzzle from go tour #43 and I don't understand why my word counter doesn't work. "print" seems to print the expected value but the tests sees only "1" regardless the count.

package main

import (
    "code.google.com/p/go-tour/wc"
"strings"
    "unicode/utf8"
)

func WordCount(s string) map[string]int {
    // explode the string into a slice without whitespaces
    ws :=  strings.Fields(s)
    //make a new map
    c := make(map[string]int)
    //iterate over each word
    for _, v := range ws{

    c[v] = utf8.RuneCountInString(v)


    }

    print( c["am"])

    return c
}

func main() {
    wc.Test(WordCount)
}

Play

You're solving the wrong problem. It doesn't ask you for the length of each word, but for the number of times each word occurs. Change

c[v] = utf8.RuneCountInString(v)

for

c[v] += 1 // or c[v]++

The problem is c[v] = utf8.RuneCountInString(v). It has two problems:

  1. You're resetting the counter for each word every time you re-encounter it. You should increment, not set.

  2. You are setting the number of runes in the word to the counter. The puzzle is "how many times a word appears in the text". so just do something like c[v] = c[v] + 1 (if the entry is empty it will default to 0)

Also, I'd normalize the text - strip punctuation marks and lowercase everything.