Last active
October 12, 2015 17:36
-
-
Save denispeplin/a59421f00074a12d0d27 to your computer and use it in GitHub Desktop.
Go exercises
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
// 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