Last active
June 17, 2018 22:15
-
-
Save zonggen/709f24340d4d9e6f4ba253772a9308de to your computer and use it in GitHub Desktop.
A Tour of Go - Maps Exercise Answer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"golang.org/x/tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
m := make (map[string]int) | |
words := strings.Fields(s) | |
for _, key := range words{ | |
_, ok := m [key] | |
if ok{ | |
m [key] ++ | |
} else { | |
m [key] = 1 | |
} | |
} | |
return m | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment