Last active
August 29, 2015 14:15
-
-
Save darul75/3a729200f89ab2b0452b to your computer and use it in GitHub Desktop.
exercise.tour.go
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
/*********************************************************/ | |
// Exercise: Readers | |
// https://tour.golang.org/methods/11 | |
package main | |
import "code.google.com/p/go-tour/reader" | |
type MyReader struct{} | |
func (r *MyReader) Read(b []byte) (int, error) { | |
n :=0 | |
for i, _ := range b { | |
b[i] = 'A' | |
n++ | |
} | |
return n, nil; | |
} | |
func main() { | |
reader.Validate(&MyReader{}) | |
} | |
/*********************************************************/ | |
// Exercise: rot13Reader | |
// https://tour.golang.org/methods/12 | |
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (rot *rot13Reader) Read(b []byte) (int, error) { | |
n, err := rot.r.Read(b) | |
for i, _ := range b { | |
b[i] = b[i] + 13 | |
} | |
return n, err | |
} | |
func main() { | |
s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} | |
/*********************************************************/ | |
// Exercise: HTTP Handlers | |
// https://tour.golang.org/methods/14 | |
package main | |
import ( | |
"log" | |
"net/http" | |
) | |
type String string | |
type Struct struct { | |
Greeting string | |
Punct string | |
Who string | |
} | |
func (s String) ServeHTTP( | |
w http.ResponseWriter, | |
r *http.Request) { | |
if r.URL.Path == "/struct" { | |
b:= make([]byte, len(s)) | |
for i := range s { | |
b[i] = s[i] | |
} | |
w.Write(b) | |
} | |
} | |
func (s *Struct) ServeHTTP( | |
w http.ResponseWriter, | |
r *http.Request) { | |
if r.URL.Path == "/struct" { | |
b:= make([]byte, len(s.Greeting)+len(s.Punct)+len(s.Who)) | |
for i := range s.Greeting { | |
b[i] = s.Greeting[i] | |
} | |
off := len(b) | |
for i := range s.Punct { | |
b[i+off] = s.Punct[i] | |
} | |
off = len(b) | |
for i := range s.Who { | |
b[i+off] = s.Who[i] | |
} | |
w.Write(b) | |
} | |
} | |
func main() { | |
// your http.Handle calls here | |
log.Fatal(http.ListenAndServe("localhost:4000", nil)) | |
} | |
/*********************************************************/ | |
// Exercise: Images | |
// https://tour.golang.org/methods/16 | |
package main | |
import ( | |
"code.google.com/p/go-tour/pic" | |
"image" | |
"image/color" | |
"math/rand" | |
) | |
type Image struct{ | |
w int | |
h int | |
} | |
// ColorModel returns the Image's color model. | |
func (i Image) ColorModel() color.Model { | |
return color.RGBAModel | |
} | |
// Bounds returns the domain for which At can return non-zero color. | |
// The bounds do not necessarily contain the point (0, 0). | |
func(i Image) Bounds() image.Rectangle { | |
return image.Rect(0, 0, i.w, i.h) | |
} | |
// At returns the color of the pixel at (x, y). | |
// At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid. | |
// At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one. | |
func (i Image) At(x, y int) color.Color { | |
return color.RGBA{uint8(rand.Intn(255)), uint8(rand.Intn(255)), uint8(rand.Intn(255)), uint8(rand.Intn(255))} | |
} | |
func main() { | |
m := Image{300,300} | |
pic.ShowImage(m) | |
} | |
/*********************************************************/ | |
// Exercise: Equivalent Binary Trees | |
// https://tour.golang.org/concurrency/8 | |
package main | |
import ( | |
"code.google.com/p/go-tour/tree" | |
"fmt" | |
) | |
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) | |
} | |
} | |
func Same(t1, t2 *tree.Tree) bool { | |
ch1:= make(chan int) | |
ch2:= make(chan int) | |
go Walk(t1, ch1) | |
go Walk(t2, ch2) | |
for i := range ch1 { | |
if i != <- ch2 { | |
return false | |
} | |
} | |
return true | |
} | |
func main() { | |
c:= make(chan int) | |
go Walk(tree.New(1), c) | |
for i := 0; i < 10; i++ { | |
fmt.Println(<-c) | |
} | |
fmt.Println(Same(tree.New(1), tree.New(1))) | |
fmt.Println(Same(tree.New(1), tree.New(2))) | |
} | |
/*********************************************************/ | |
// Exercise: Web Crawler | |
// https://tour.golang.org/concurrency/9 | |
package main | |
import ( | |
"fmt" | |
) | |
type Fetcher interface { | |
Fetch(url string) (body string, urls []string, err error) | |
} | |
var dones map[string]bool | |
func DoCrawl(url string, f Fetcher, ch chan []string) { | |
body, urls, err := f.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, body) | |
ch <- urls | |
} | |
func Crawl(url string, depth int, fetcher Fetcher) { | |
urls := make(chan []string) | |
if depth <= 0 { | |
return | |
} | |
go DoCrawl(url, fetcher, urls) | |
depth -=1 | |
dones[url] = true | |
for depth > 0 { | |
for _, u := range <- urls { | |
if _, done := dones[u] ; !done { | |
go DoCrawl(u, fetcher, urls) | |
dones[u] = true | |
} | |
} | |
depth-- | |
} | |
close(urls) | |
return | |
} | |
func main() { | |
dones = make(map[string]bool) | |
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/", | |
}, | |
}, | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment