Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Tour of Go: Exercise: rot13Reader, https://go-tour-jp.appspot.com/methods/23
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r rot13Reader) Read(b []byte) (int, error) {
n, err := r.r.Read(b)
for i := 0; i < n; i++ {
b[i] = rot13(b[i])
}
return n, err
}
func rot13(b byte) byte {
if 'a' <= b && b <= 'z' {
base := b - 'a'
b = 'a' + (base+13)%26
} else if 'A' <= b && b <= 'Z' {
base := b - 'A'
b = 'A' + (base+13)%26
}
return b
}
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