Skip to content

Instantly share code, notes, and snippets.

@icub3d
Created June 23, 2012 16:27
Show Gist options
  • Save icub3d/2978920 to your computer and use it in GitHub Desktop.
Save icub3d/2978920 to your computer and use it in GitHub Desktop.
My solutions to "A Tour of Go"
package main
import (
"fmt"
"math/cmplx"
)
func Cbrt(x complex128) complex128 {
z := complex128(1.0)
for i := 0; i < 10; i++ {
z = z - ((z*z*z)-x)/(3*(z*z))
}
return z
}
func main() {
fmt.Println(Cbrt(2), cmplx.Pow(2, 1.0/3.0))
}
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) {
WalkHelper(t, ch)
close(ch)
}
func WalkHelper(t *tree.Tree, ch chan int) {
if t.Left != nil {
WalkHelper(t.Left, ch)
}
ch <- t.Value
if t.Right != nil {
WalkHelper(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 {
r1, ok1 := <-ch1
r2, ok2 := <-ch2
if ok1 == ok2 && ok2 == false {
return true
}
if ok1 != ok2 {
return false
}
if r1 != r2 {
return false
}
}
return true
}
func main() {
fmt.Println(tree.New(1))
ch := make(chan int)
go Walk(tree.New(1), ch)
for value := range ch {
fmt.Println(value)
}
fmt.Println(Same(tree.New(1), tree.New(1)))
fmt.Println(Same(tree.New(1), tree.New(2)))
}
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
prev := -1
cur := 0
return func() int {
ret := 0
if prev == -1 {
prev = 0
cur = 1
} else {
ret = cur
cur = cur + prev
prev = ret
}
return ret
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
package main
import (
"code.google.com/p/go-tour/pic"
"image"
"image/color"
)
type Image struct {
w, h int
}
func (i Image) Bounds() image.Rectangle {
return image.Rect(0, 0, i.w, i.h)
}
func (i Image) At(x, y int) color.Color {
return color.RGBA{uint8(x * y), uint8(x * y), 255, 255}
}
func (i Image) ColorModel() color.Model {
return color.RGBAModel
}
func main() {
m := Image{400, 400}
pic.ShowImage(m)
}
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r rot13Reader) Read(p []byte) (n int, err error) {
n, err = r.r.Read(p)
if err != nil {
return n, err
}
var A, a, M, m, N, bn, Z, z byte
A, a, M, m, N, bn, Z, z = 65, 97, 77, 109, 78, 110, 90, 122
for i := 0; i < n; i++ {
if (p[i] >= A && p[i] <= M) || (p[i] >= a && p[i] <= m) {
p[i] += 13
} else if (p[i] >= N && p[i] <= Z) || (p[i] >= bn && p[i] <= z) {
p[i] -= 13
}
}
return n, err
}
func main() {
s := strings.NewReader(
"Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
package main
import (
"fmt"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
Fetch(url string) (body string, urls []string, err error)
}
type fetchResults struct {
depth int
urls []string
}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher) {
results := make(chan fetchResults)
workers := 1
visited := map[string]bool{url: true}
worker := func(url string, depth int) {
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("found: %s %q\n", url, body)
}
results <- fetchResults{depth, urls}
}
go worker(url, 0)
for workers > 0 {
result := <-results
workers--
if result.depth > depth {
continue
}
for _, url := range result.urls {
if !visited[url] {
visited[url] = true
workers++
go worker(url, result.depth+1)
}
}
}
}
func main() {
Crawl("http://golang.org/", 4, fetcher)
}
// fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResult
type fakeResult struct {
body string
urls []string
}
func (f *fakeFetcher) Fetch(url string) (string, []string, error) {
if res, ok := (*f)[url]; ok {
return res.body, res.urls, nil
}
return "", nil, fmt.Errorf("not found: %s", url)
}
// fetcher is a populated fakeFetcher.
var fetcher = &fakeFetcher{
"http://golang.org/": &fakeResult{
"The Go Programming Language",
[]string{
"http://golang.org/pkg/",
"http://golang.org/cmd/",
},
},
"http://golang.org/pkg/": &fakeResult{
"Packages",
[]string{
"http://golang.org/",
"http://golang.org/cmd/",
"http://golang.org/pkg/fmt/",
"http://golang.org/pkg/os/",
},
},
"http://golang.org/pkg/fmt/": &fakeResult{
"Package fmt",
[]string{
"http://golang.org/",
"http://golang.org/pkg/",
},
},
"http://golang.org/pkg/os/": &fakeResult{
"Package os",
[]string{
"http://golang.org/",
"http://golang.org/pkg/",
},
},
}
@karthikworld
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment