Skip to content

Instantly share code, notes, and snippets.

@Ovid
Created December 23, 2016 10:47
Show Gist options
  • Save Ovid/7b64067fa4de2f7553d48bb9746b4574 to your computer and use it in GitHub Desktop.
Save Ovid/7b64067fa4de2f7553d48bb9746b4574 to your computer and use it in GitHub Desktop.
Rot13 in golang (exercise from tour)
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
reader io.Reader
}
func (rot *rot13Reader) Read(p []byte) (int, error) {
n, err := rot.reader.Read(p)
for i, char := range p {
if (char >= 'A' && char < 'N') || (char >= 'a' && char < 'n') {
p[i] += 13
} else if (char > 'M' && char <= 'Z') || (char > 'm' && char <= 'z') {
p[i] -= 13
}
}
return n, err
}
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