Skip to content

Instantly share code, notes, and snippets.

@snahor
Last active December 22, 2015 02:09
Show Gist options
  • Save snahor/6401668 to your computer and use it in GitHub Desktop.
Save snahor/6401668 to your computer and use it in GitHub Desktop.
golang tour exercises
package main
import (
"strings"
"code.google.com/p/go-tour/wc"
)
func WordCount(s string) map[string]int {
m := make(map[string]int);
for _, vv := range strings.Fields(s) {
m[vv] = m[vv] + 1
}
return m
}
func main() {
wc.Test(WordCount)
}
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x := -1
y := 1
z := 0
return func() int {
z = x + y
x = y
y = z
return z
}
}
func main() {
f := fibonacci()
for i := 0; i < 20; i++ {
fmt.Printf("fibonacci(%d) = %d\n", i, f())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment