Skip to content

Instantly share code, notes, and snippets.

@uraimo
Last active December 18, 2015 03:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uraimo/5721702 to your computer and use it in GitHub Desktop.
Save uraimo/5721702 to your computer and use it in GitHub Desktop.
A Tour of Go: Rot13 Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (reader rot13Reader) Read(p []byte) (n int, err error){
alf := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
n,e := reader.r.Read(p)
for i,b := range(p) {
ru := rune(b)
idx:=strings.IndexRune(alf,ru)
if idx>0 {
p[i] = alf[(idx+13)%len(alf)]
}
}
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