読者です 読者をやめる 読者になる 読者になる

A Tour of Go "moretypes/23"

package main

import (
	"fmt"
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	fmt.Println(strings.Fields(s))
	var m = map[string]int{}
	for _, v := range strings.Split(s, " ") {
		_, exist := m[v]
		if exist {
			m[v] += 1
		} else {
			m[v] = 1
		}
	}

	return m
}

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

/usr/local/Cellar/go/1.8.1/libexec/bin/go run /Users/seijiro/Dropbox/code/GoglandProjects/first/10.go
[I am learning Go!]
PASS
f("I am learning Go!") =
map[string]int{"I":1, "am":1, "learning":1, "Go!":1}
[The quick brown fox jumped over the lazy dog.]
PASS
f("The quick brown fox jumped over the lazy dog.") =
map[string]int{"fox":1, "lazy":1, "dog.":1, "The":1, "quick":1, "brown":1, "jumped":1, "over":1, "the":1}
[I ate a donut. Then I ate another donut.]
PASS
f("I ate a donut. Then I ate another donut.") =
map[string]int{"another":1, "I":2, "ate":2, "a":1, "donut.":2, "Then":1}
[A man a plan a canal panama.]
PASS
f("A man a plan a canal panama.") =
map[string]int{"man":1, "a":2, "plan":1, "canal":1, "panama.":1, "A":1}

Process finished with exit code 0