Last active
August 29, 2015 14:03
-
-
Save emre/e87d6e382cacff129522 to your computer and use it in GitHub Desktop.
golang tour solutions
This file contains hidden or 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, y := 0, 1 | |
return func() int { | |
x, y = y, x+y | |
return y | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
This file contains hidden or 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" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
next, prev := float64(1), float64(0) | |
for math.Abs(prev-next) > 1e-15 { | |
prev, next = next, next-(next*next-x)/(2*next) | |
} | |
return prev | |
} | |
func main() { | |
fmt.Println(Sqrt(65536)) | |
fmt.Println(math.Sqrt(65536)) | |
} |
This file contains hidden or 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 ( | |
"code.google.com/p/go-tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
resultset := make(map[string]int) | |
for _, word := range strings.Fields(s) { | |
resultset[word]++ | |
} | |
return resultset | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment