Created
November 7, 2017 16:22
-
-
Save jcreager/61cd3a666e730b3689f240f8f2901dec to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Maps
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 { | |
words := strings.Fields(s) | |
var count map[string]int | |
count = make(map[string]int) | |
for i := 0; i < len(words); i++ { | |
v, ok := count[words[i]] | |
if ok != true { | |
count[words[i]] = 1 | |
} else { | |
count[words[i]] = v + 1 | |
} | |
} | |
return count | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment