Last active
July 27, 2017 12:28
-
-
Save antonu17/6091fcc224760c5e8e8f62c3e1ebe887 to your computer and use it in GitHub Desktop.
Tour of Go Excercises
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
| 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) { | |
| 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 { | |
| c1 := make(chan int) | |
| c2 := make(chan int) | |
| go Walk(t1, c1) | |
| go Walk(t2, c2) | |
| for i := 0; i < 10; i++ { | |
| v1, v2 := <-c1, <-c2 | |
| if v1 != v2 { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| func main() { | |
| ch := make(chan int) | |
| go Walk(tree.New(1), ch) | |
| for i := 0; i < 10; i++ { | |
| fmt.Printf("%v ", <-ch) | |
| } | |
| fmt.Println() | |
| fmt.Println(Same(tree.New(1), tree.New(1))) | |
| fmt.Println(Same(tree.New(1), tree.New(2))) | |
| } |
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
| package main | |
| import ( | |
| "fmt" | |
| "math" | |
| ) | |
| type ErrNegativeSqrt float64 | |
| var precision = math.Pow10(5) // 5 decimal places | |
| func (e ErrNegativeSqrt) Error() string { | |
| return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e)) | |
| } | |
| func Sqrt(x float64) (float64, error) { | |
| if x < 0 { | |
| return 0.0, ErrNegativeSqrt(x) | |
| } | |
| prev, z := 0.0, 1.0 | |
| for { | |
| if prev, z = z, z-(z*z-x)/(2*z); int(prev*precision) == int(z*precision) { | |
| return z, nil | |
| } | |
| } | |
| } | |
| func main() { | |
| fmt.Println(Sqrt(2)) | |
| fmt.Println(Sqrt(-2)) | |
| } |
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
| package main | |
| import "fmt" | |
| // fibonacci is a function that returns | |
| // a function that returns an int. | |
| func fibonacci() func() int { | |
| first, second := 0, 1 | |
| return func() int { | |
| current := first | |
| first, second = second, first+second | |
| return current | |
| } | |
| } | |
| func main() { | |
| f := fibonacci() | |
| for i := 0; i < 15; i++ { | |
| fmt.Println(f()) | |
| } | |
| } |
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
| package main | |
| import ( | |
| "golang.org/x/tour/pic" | |
| "image" | |
| "image/color" | |
| ) | |
| type Image struct{} | |
| func (i Image) Bounds() image.Rectangle { | |
| return image.Rect(0, 0, 255, 255) | |
| } | |
| func (i Image) ColorModel() color.Model { | |
| return color.RGBAModel | |
| } | |
| func (i Image) At(x, y int) color.Color { | |
| return color.RGBA{uint8(x - y), uint8(y + x), 90, 255} | |
| } | |
| func main() { | |
| m := Image{} | |
| pic.ShowImage(m) | |
| } |
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
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| func Sqrt(x float64) float64 { | |
| z := 1.0 | |
| for i := 0; i < 10; i++ { | |
| z = z - (z*z-x)/(2*z) | |
| } | |
| return z | |
| } | |
| func main() { | |
| fmt.Println(Sqrt(2)) | |
| } |
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
| package main | |
| import ( | |
| "fmt" | |
| "math" | |
| ) | |
| var precision = math.Pow10(5) // 5 decimal places | |
| func Sqrt(x float64) float64 { | |
| prev, z := 0.0, 1.0 | |
| for { | |
| if prev, z = z, z-(z*z-x)/(2*z); int(prev*precision) == int(z*precision) { | |
| return z | |
| } | |
| } | |
| } | |
| func main() { | |
| fmt.Println(Sqrt(2)) | |
| } |
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
| package main | |
| import ( | |
| "golang.org/x/tour/wc" | |
| "strings" | |
| ) | |
| func WordCount(s string) map[string]int { | |
| var m = make(map[string]int) | |
| for _, word := range (strings.Fields(s)) { | |
| m[word] = m[word] + 1 | |
| } | |
| return m | |
| } | |
| func main() { | |
| wc.Test(WordCount) | |
| } |
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
| package main | |
| import "golang.org/x/tour/reader" | |
| type MyReader struct{} | |
| // TODO: Add a Read([]byte) (int, error) method to MyReader. | |
| func (r 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 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
| package main | |
| import ( | |
| "io" | |
| "os" | |
| "strings" | |
| ) | |
| type rot13Reader struct { | |
| r io.Reader | |
| } | |
| func rot13(r byte) byte { | |
| switch { | |
| case r >= 'A' && r <= 'Z': | |
| return 'A' + (r-'A'+13)%26 | |
| case r >= 'a' && r <= 'z': | |
| return 'a' + (r-'a'+13)%26 | |
| } | |
| return r | |
| } | |
| func (r *rot13Reader) Read(b []byte) (int, error) { | |
| n, err := r.r.Read(b) | |
| for i, v := range b { | |
| b[i] = rot13(v) | |
| } | |
| return n, err | |
| } | |
| func main() { | |
| s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
| r := rot13Reader{s} | |
| io.Copy(os.Stdout, &r) | |
| } |
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
| package main | |
| import "golang.org/x/tour/pic" | |
| func Pic(dx, dy int) [][]uint8 { | |
| var pic = make([][]uint8, dy) | |
| for y := 0; y < dy; y++ { | |
| var row = make([]uint8, dx) | |
| for x := 0; x < dx; x++ { | |
| row[x] = uint8(x ^ y) // (x+y)/2 or x*y or x^y | |
| } | |
| pic[y] = row | |
| } | |
| return pic | |
| } | |
| func main() { | |
| pic.Show(Pic) | |
| } |
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
| package main | |
| import "fmt" | |
| type IPAddr [4]byte | |
| // TODO: Add a "String() string" method to IPAddr. | |
| func (addr IPAddr) String() string { | |
| return fmt.Sprintf("%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[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) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment