Last active
August 29, 2015 14:00
-
-
Save aseigneurin/11324117 to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Rot13 Reader
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 ( | |
"code.google.com/p/go-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.Left != nil { | |
fmt.Println("Going left") | |
Walk(t.Left, ch) | |
ch <- t.Value | |
fmt.Println("Value (left): ", t.Value) | |
fmt.Println("Up (left)") | |
} else if t.Right != nil { | |
ch <- t.Value | |
fmt.Println("Value (right): ", t.Value) | |
fmt.Println("Going right") | |
Walk(t.Right, ch) | |
fmt.Println("Up (right)") | |
} else { | |
fmt.Println("Else") | |
ch <- t.Value | |
fmt.Println("Value (node): ", t.Value) | |
fmt.Println("Up (node)") | |
} | |
} | |
// Same determines whether the trees | |
// t1 and t2 contain the same values. | |
func Same(t1, t2 *tree.Tree) bool { | |
return false | |
} | |
func main() { | |
ch := make(chan int, 10) | |
go Walk(tree.New(1), ch) | |
for v := range ch { | |
fmt.Println(v) | |
} | |
} |
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 (this rot13Reader) Read(p []byte) (n int, err error) { | |
n, err = this.r.Read(p) | |
fmt.Println(n) | |
for i := 0 ; i < n ; i++ { | |
c := p[i] | |
if ( c >= 'A' && c <= 'M' ) || ( c >= 'a' && c <= 'm' ) { | |
p[i] = c + 13 | |
} else { | |
p[i] = c - 13 | |
} | |
} | |
return n, err | |
} | |
func main() { | |
s := strings.NewReader( | |
"Lbh penpxrq gur pbqr!") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment