Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created April 2, 2012 02:20
Show Gist options
  • Save tetsuok/2280069 to your computer and use it in GitHub Desktop.
Save tetsuok/2280069 to your computer and use it in GitHub Desktop.
An answer of the exercise: Maps on a tour of Go
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
m := make(map[string]int)
a := strings.Fields(s)
for _, v := range a {
m[v]++
}
return m
}
func main() {
wc.Test(WordCount)
}
@joelabreurojas
Copy link

joelabreurojas commented Mar 13, 2024

package main

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

func WordCount(s string) map[string]int {
	count := make(map[string]int)
	
	for _, word := range strings.Fields(s) {
		count[word]++
	}
	
	return count
}

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

@filipcvejic
Copy link

package main

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

func WordCount(s string) map[string]int {
	counts := make(map[string]int)
	words := strings.Fields(s)
	for _, word := range words {
		counts[word]++
	}
	
	return counts
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment