Skip to content

Instantly share code, notes, and snippets.

@yuseinishiyama
Created January 30, 2019 21:10
Show Gist options
  • Save yuseinishiyama/4613a7591ecce070ee36da15cf7c814b to your computer and use it in GitHub Desktop.
Save yuseinishiyama/4613a7591ecce070ee36da15cf7c814b to your computer and use it in GitHub Desktop.
A Tour of Go: rot13Reader
// https://tour.golang.org/methods/23
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r *rot13Reader) Read(b []byte) (int, error) {
n, e := r.r.Read(b)
for i, v := range b[:n] {
switch {
case 'A' <= v && v <= 'M' || 'a' <= v && v <= 'm':
b[i] += 13
case 'N' <= v && v <= 'Z' || 'n' <= v && v <= 'z':
b[i] -= 13
}
}
return n, e
}
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