This is a creation in Article, where the information may have evolved or changed.
Do an exercise first: HTTP://TOUR.GOLANGTC.COM/MORETYPES/18
Exercise: Map
Implement ' WordCount '. It should return a map containing the s
number of "words" in each. wc.Test
The function executes a test case against the function and prints the success or failure.
You'll find strings. fields are very helpful.
My Code:
Package Mainimport ( "CODE.GOOGLE.COM/P/GO-TOUR/WC" "Strings" ) func WordCount (s string) Map[string]int { str: = strings. Fields (s)//divide the string into the word array m: = Make (Map[string]int) for I:=0;i<len (str); I++{if m[str[i]]==0{ m[str[i]]= 1}else{ m[str[i]]++} }return M}func main () {WC. Test (WordCount)}
Operation Result:
PASS F ("I am learning go!") = map[string]int{"I": 1, "AM": 1, "Learning": 1, "go!" : 1}pass F ("The quick brown fox jumped over the lazy dog.") = map[string]int{"Quick": 1, "Fox": 1, "jumped": 1, "the": 1, " The ": 1," Over ": 1," lazy ": 1," dog. " : 1, "Brown": 1}pass f ("I ate a donut. Then I ate another donut. ") = map[string]int{"ate": 2, "a": 1, "Donut." : 2, "then": 1, "another": 1, "I": 2}pass F ("A mans a plan A canal Panama.") = map[string]int{"A": 1, "Man": 1, "A": 2, "plan" : 1, "Canal": 1, "Panama." : 1}