Last active
December 22, 2015 02:09
-
-
Save snahor/6401668 to your computer and use it in GitHub Desktop.
golang tour 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
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) | |
} |
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
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