Skip to content

Instantly share code, notes, and snippets.

@vjames19
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjames19/10676502 to your computer and use it in GitHub Desktop.
Save vjames19/10676502 to your computer and use it in GitHub Desktop.
golang tour exercise solutions

Golang Tour Exercise solutions

package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
var z, s float64 = 1, 0
for {
z = z - (z*z - x) / (2*z)
if math.Abs(s - z) < 1e-16 {
break;
}
s = z
}
return s
}
func main() {
fmt.Println(Sqrt(25))
}
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for y := 0; y < dy; y++ {
xslice := make([]uint8, dx)
for x:=0; x < dx; x++ {
xslice[x] = uint8((x*y)/16)
}
pic[y] = xslice
}
return pic
}
func main() {
pic.Show(Pic)
}
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
wordCount := make(map[string]int)
for _, word := range words {
wordCount[word] = wordCount[word] + 1
}
return wordCount
}
func main() {
wc.Test(WordCount)
}
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 x
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
package main
import "fmt"
import "math/cmplx"
func Cbrt(x complex128) complex128 {
var s, z complex128 = 0, 1
for {
z = z - ((cmplx.Pow(z, 3) - x) / (3 * cmplx.Pow(z, 2)))
if cmplx.Abs(s - z) < 1e-16 {
break;
}
s = z
}
return s
}
func main() {
fmt.Println(Cbrt(2))
}
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot sqrt negative number: %g", float64(e))
}
func Sqrt(x float64) (float64, error) {
if x < 0 {
return 0, ErrNegativeSqrt(x)
}
var z, s float64 = 1, 0
for {
z = z - (z*z - x) / (2*z)
if math.Abs(s - z) < 1e-15 {
break;
}
s = z
}
return s, nil
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
package main
import (
"fmt"
"net/http"
)
type String string
func (s String) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
fmt.Fprint(rw, s)
}
type Struct struct {
Greeting string
Punct string
Who string
}
func (s Struct) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, "%s %s %s", s.Greeting, s.Punct, s.Who)
}
func main() {
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
http.ListenAndServe("localhost:9000", nil)
}
package main
import (
"code.google.com/p/go-tour/pic"
"image"
"image/color"
)
type Image struct {
width int
height int
}
func (img Image) Bounds() image.Rectangle {
return image.Rect(0, 0, img.width, img.height)
}
func (img Image) At(x, y int) color.Color {
v := byte(x * y)
return color.RGBA{v, v, 255, 255}
}
func (img Image) ColorModel() color.Model {
return color.RGBAModel
}
func main() {
m := Image{400, 400}
pic.ShowImage(m)
}
package main
import "code.google.com/p/go-tour/tree"
import "fmt"
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
walk(t, ch)
close(ch)
}
func walk(t *tree.Tree, ch chan int) {
if t != nil {
walk(t.Left, ch)
ch <- t.Value
walk(t.Right, ch)
}
}
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
ch1 := make(chan int)
ch2 := make(chan int)
go Walk(t1, ch1)
go Walk(t2, ch2)
for v := range ch1 {
if v != <-ch2 {
return false
}
}
return true
}
func main() {
ch := make(chan int)
go Walk(tree.New(1), ch)
for v := range ch {
fmt.Print(v)
}
fmt.Println(Same(tree.New(1), tree.New(2)))
fmt.Println(Same(tree.New(1), tree.New(1)))
fmt.Println(Same(&tree.Tree{Value: 10}, &tree.Tree{Value: 11}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment