Created
December 23, 2016 10:47
-
-
Save Ovid/7b64067fa4de2f7553d48bb9746b4574 to your computer and use it in GitHub Desktop.
Rot13 in golang (exercise from tour)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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