My solutions for the exersices of A Tour of Go.
Last active
March 23, 2020 18:51
-
-
Save puyihua/02e7983791c86349078c610053f19446 to your computer and use it in GitHub Desktop.
A Tour of Go - Exersices
This file contains 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 ( | |
"io" | |
"os" | |
"strings" | |
// "fmt" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (rStruct rot13Reader) Read(b []byte) (int, error) { | |
n, err := rStruct.r.Read(b) | |
for i,_ := range b { | |
if (b[i] > 64 && b[i] < 78) ||(b[i] > 96 && b[i] < 110) { | |
b[i] += 13 | |
} else { | |
b[i] -= 13 | |
} | |
} | |
//fmt.Printf("%q , %v", b[:n], err) | |
return n, err | |
} | |
func main() { | |
s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} |
This file contains 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 ( | |
"golang.org/x/tour/tree" | |
"fmt" | |
) | |
// Walk walks the tree t sending all values | |
// from the tree to the channel ch. | |
func Walk(t *tree.Tree, ch chan int) { | |
inorder(t, ch) | |
close(ch) | |
} | |
func inorder(t *tree.Tree, ch chan int) { | |
if t.Left != nil { | |
inorder(t.Left, ch) | |
} | |
ch <- t.Value | |
if t.Right != nil { | |
inorder(t.Right, ch) | |
} | |
} | |
// Same determines whether the trees | |
// t1 and t2 contain the same values. | |
func Same(t1, t2 *tree.Tree) bool { | |
ch1, ch2 := make(chan int, 10), make(chan int, 10) | |
go Walk(t1, ch1) | |
go Walk(t2, ch2) | |
for i := 0; i< 10; i++ { | |
x, y := <- ch1, <-ch2 | |
if ( x != y ) { | |
return false | |
} | |
} | |
return true | |
} | |
func main() { | |
if Same(tree.New(1), tree.New(1)) { | |
fmt.Println("Same") | |
} else { | |
fmt.Println("Not Same") | |
} | |
if Same(tree.New(1), tree.New(2)) { | |
fmt.Println("Same") | |
} else { | |
fmt.Println("Not Same") | |
} | |
} |
This file contains 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" | |
) | |
type ErrNegativeSqrt float64 | |
func (e ErrNegativeSqrt) Error() string { | |
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e)) | |
} | |
func Sqrt(x float64) (float64, error) { | |
if (x >= 0) { | |
var z float64 = 1.0 | |
for i :=0; i < 10; i ++ { | |
z -= (z*z - x) / (2*z) | |
} | |
return z, nil | |
} else { | |
var e ErrNegativeSqrt = ErrNegativeSqrt(x) | |
return x, &e | |
} | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} |
This file contains 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 ( | |
"golang.org/x/tour/pic" | |
"image/color" | |
"image" | |
) | |
//type Image interface { | |
// ColorModel() color.Model | |
// Bounds() Rectangle | |
// At(x, y int) color.Color | |
//} | |
type Image struct{ | |
w int | |
h int | |
} | |
func (Image) ColorModel() color.Model { | |
return color.RGBAModel | |
} | |
func (i Image) Bounds() image.Rectangle { | |
return image.Rect(0, 0, i.w, i.h) | |
} | |
func (i Image) At(x, y int) color.Color { | |
dx, dy := i.w, i.h | |
var pic = make([][]uint8, dy); | |
for i := 0; i < dy; i++ { | |
pic[i] = make([]uint8, dx); | |
} | |
for i := 0; i < dy; i++ { | |
for j := 0; j < dx; j++ { | |
pic[i][j] = uint8(i^j) | |
} | |
} | |
v := pic[x][y] | |
return color.RGBA{v, v, 255, 255} | |
} | |
func main() { | |
m := Image{100, 100} | |
pic.ShowImage(m) | |
} |
This file contains 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 ( | |
"golang.org/x/tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
m := make(map[string]int) | |
for _, v:= range strings.Fields(s) { | |
_, ok := m[v] | |
if ok { | |
m[v] ++ | |
} else { | |
m[v] = 1 | |
} | |
} | |
return m | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
This file contains 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 "golang.org/x/tour/reader" | |
type MyReader struct{} | |
// TODO: Add a Read([]byte) (int, error) method to MyReader. | |
func (MyReader) Read(b []byte) (int, error) { | |
for i := 0; i < len(b); i++ { | |
b[i] = 'A' | |
} | |
return len(b), nil | |
} | |
func main() { | |
reader.Validate(MyReader{}) | |
} |
This file contains 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 "golang.org/x/tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
var pic = make([][]uint8, dy); | |
for i := 0; i < dy; i++ { | |
pic[i] = make([]uint8, dx); | |
} | |
for i := 0; i < dy; i++ { | |
for j := 0; j < dx; j++ { | |
pic[i][j] = uint8(i^j) | |
} | |
} | |
return pic | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
This file contains 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" | |
type IPAddr [4]byte | |
// TODO: Add a "String() string" method to IPAddr. | |
func (ip IPAddr) String () string { | |
return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3]) | |
} | |
func main() { | |
hosts := map[string]IPAddr{ | |
"loopback": {127, 0, 0, 1}, | |
"googleDNS": {8, 8, 8, 8}, | |
} | |
for name, ip := range hosts { | |
fmt.Printf("%v: %v\n", name, ip) | |
} | |
} |
This file contains 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" | |
"sync" | |
) | |
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 SafeMap struct{ | |
m map[string]bool | |
mux sync.Mutex | |
} | |
func (sm *SafeMap) has(url string) bool{ | |
sm.mux.Lock() | |
defer sm.mux.Unlock() | |
_, ok := sm.m[url] | |
if ok { | |
return true | |
} else { | |
sm.m[url] = true | |
return false | |
} | |
} | |
// Crawl uses fetcher to recursively crawl | |
// pages starting with url, to a maximum of depth. | |
func Crawl(url string, depth int, fetcher Fetcher, sm SafeMap) { | |
// TODO: Fetch URLs in parallel. | |
// TODO: Don't fetch the same URL twice. | |
if sm.has(url) { | |
return | |
} | |
// This implementation doesn't do either: | |
if depth <= 0 { | |
return | |
} | |
body, urls, err := fetcher.Fetch(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Printf("found: %s %q\n", url, body) | |
var wg sync.WaitGroup | |
//wg := sync.WaitGroup{} | |
for _, u := range urls { | |
wg.Add(1) | |
u2 := u // make a copy of the outter variable | |
go func() { | |
defer wg.Done() | |
// make sure Done() is executed even the goroutine fails | |
Crawl(u2, depth-1, fetcher, sm) | |
}() | |
} | |
wg.Wait() // don't forget | |
return | |
} | |
func main() { | |
sm := SafeMap{m: make(map[string]bool)} | |
Crawl("https://golang.org/", 4, fetcher, sm) | |
} | |
// 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{ | |
"https://golang.org/": &fakeResult{ | |
"The Go Programming Language", | |
[]string{ | |
"https://golang.org/pkg/", | |
"https://golang.org/cmd/", | |
}, | |
}, | |
"https://golang.org/pkg/": &fakeResult{ | |
"Packages", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/cmd/", | |
"https://golang.org/pkg/fmt/", | |
"https://golang.org/pkg/os/", | |
}, | |
}, | |
"https://golang.org/pkg/fmt/": &fakeResult{ | |
"Package fmt", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/pkg/", | |
}, | |
}, | |
"https://golang.org/pkg/os/": &fakeResult{ | |
"Package os", | |
[]string{ | |
"https://golang.org/", | |
"https://golang.org/pkg/", | |
}, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment