Skip to content

Instantly share code, notes, and snippets.

@anna-hope
Created March 29, 2022 03:56
Show Gist options
  • Save anna-hope/5bcf5dfa9cd9bd9a7f09fb55b756f095 to your computer and use it in GitHub Desktop.
Save anna-hope/5bcf5dfa9cd9bd9a7f09fb55b756f095 to your computer and use it in GitHub Desktop.
go rot13
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r13reader rot13Reader) Read(bytes []byte) (int, error) {
n, err := r13reader.r.Read(bytes)
if err != nil {
if err == io.EOF {
return n, io.EOF
} else {
panic("something went horribly wrong")
}
}
for n, c := range bytes {
if (c > 77 && c < 91) || c > 109 {
c -= 13
} else if c > 64 || c > 96 {
c += 13
}
bytes[n] = c
}
return n, nil
}
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