Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aseigneurin/11324117 to your computer and use it in GitHub Desktop.
Save aseigneurin/11324117 to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Rot13 Reader
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)
}
}
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