Last active
June 3, 2018 09:11
-
-
Save nikotung/ae2cace62f0c7c1c83018a4e5e670f8f to your computer and use it in GitHub Desktop.
go exercise
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" | |
"golang.org/x/tour/tree" | |
) | |
// Walk walks the tree t sending all values | |
// from the tree to the channel ch. | |
func Walk(t *tree.Tree, ch chan int) { | |
revers(t, ch) | |
close(ch) | |
} | |
func revers(t *tree.Tree, ch chan int) { | |
if t == nil { | |
return | |
} | |
revers(t.Left, ch) | |
ch <- t.Value | |
revers(t.Right, ch) | |
} | |
// Same determines whether the trees | |
// t1 and t2 contain the same values. | |
func Same(t1, t2 *tree.Tree) bool { | |
c1, c2 := make(chan int), make(chan int) | |
go Walk(t1, c1) | |
go Walk(t2, c2) | |
for { | |
v1, ok1 := <-c1 | |
v2, ok2 := <-c2 | |
if !ok1 || !ok2 { | |
return ok1 == ok2 | |
} | |
if v1 != v2 { | |
return false | |
} | |
} | |
} | |
func main() { | |
c := make(chan int) | |
go Walk(tree.New(1), c) | |
//print the tree | |
for i := range c { | |
fmt.Println(i) | |
} | |
if Same(tree.New(1), tree.New(1)) { | |
fmt.Println("tree.New(1) should equal to tree.New(1)") | |
} | |
if !Same(tree.New(1), tree.New(2)) { | |
fmt.Println("tree.New(1) should not equal to 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 ( | |
"golang.org/x/tour/pic" | |
"image" | |
"image/color" | |
) | |
type Image struct { | |
W, H int | |
} | |
func (p Image) ColorModel() color.Model { return color.RGBAModel } | |
func (p Image) Bounds() image.Rectangle { return image.Rect(0, 0, p.W, p.H) } | |
func (p Image) At(x, y int) color.Color { | |
return p.RGBAAt(x, y) | |
} | |
func (p Image) RGBAAt(x, y int) color.Color { | |
return color.NRGBA{(uint8(x) + uint8(y)) / 2, uint8(x) + uint8(y), 255, 255} | |
} | |
func main() { | |
m := Image{300, 100} | |
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 "golang.org/x/tour/reader" | |
type MyReader struct{} | |
// TODO: Add a Read([]byte) (int, error) method to MyReader. | |
func (r MyReader) Read(b []byte) (n int, err error) { | |
infinity := "A" | |
n = copy(b, infinity[:]) | |
return | |
} | |
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" | |
) | |
func rot13(b byte) byte { | |
var a byte | |
switch { | |
case 'a' <= b && b <= 'z': | |
a = 'a' | |
case b >= 'A' && b <= 'Z': | |
a = 'A' | |
default: | |
return b | |
} | |
return (b-a+13)%26 + a | |
} | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (r rot13Reader) Read(b []byte) (n int, err error) { | |
n, err = r.r.Read(b) | |
for i := 0; i < n; i++ { | |
b[i] = rot13(b[i]) | |
} | |
return | |
} | |
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 { | |
x := make([][]uint8, dx) | |
for i := 0; i < dx; i++ { | |
x[i] = make([]uint8, dy) | |
for j := 0; j < dy; j++ { | |
//x[i][j] = uint8(i) * uint8(j) | |
x[i][j] = (uint8(i) + uint8(j)) / 2 | |
} | |
} | |
return x | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://tour.go-zh.org/methods/23