Skip to content

Instantly share code, notes, and snippets.

@emre
Last active August 29, 2015 14:03
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 emre/e87d6e382cacff129522 to your computer and use it in GitHub Desktop.
Save emre/e87d6e382cacff129522 to your computer and use it in GitHub Desktop.
golang tour solutions
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())
}
}
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))
}
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