Skip to content

Instantly share code, notes, and snippets.

@takatoshiono
Last active September 3, 2016 10:09
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 takatoshiono/6ccedf857ead078a9ec89f8a26a80de2 to your computer and use it in GitHub Desktop.
Save takatoshiono/6ccedf857ead078a9ec89f8a26a80de2 to your computer and use it in GitHub Desktop.
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)
}
@takatoshiono
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment