Skip to content

Instantly share code, notes, and snippets.

@denispeplin
Last active October 12, 2015 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denispeplin/a59421f00074a12d0d27 to your computer and use it in GitHub Desktop.
Save denispeplin/a59421f00074a12d0d27 to your computer and use it in GitHub Desktop.
Go exercises
// exercise-maps
// https://tour.golang.org/moretypes/19
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
result := make(map[string]int)
for _, element := range strings.Fields(s) {
result[element]++
}
return result
}
func main() {
wc.Test(WordCount)
}
// exercise-fibonacci-closure
// https://tour.golang.org/moretypes/22
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
f, g := 0, 1
return func() int {
f, g = g, f+g
return f
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment