Created
January 18, 2018 21:58
-
-
Save c-yan/78b9d35a14f2212b0459f2a928241c9b to your computer and use it in GitHub Desktop.
[A Tour of Go] Exercise: rot13Reader
This file contains 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 { | |
r io.Reader | |
} | |
func rot13(b byte) byte { | |
if b <= 'A' || b > 'z' { | |
return b | |
} | |
if b >= 'a' { | |
return (((b - 'a') + 13) % 26) + 'a' | |
} else { | |
return (((b - 'A') + 13) % 26) + 'A' | |
} | |
} | |
func (r rot13Reader) Read(p []byte) (n int, err error) { | |
n, err = r.r.Read(p) | |
for i := range p { | |
p[i] = rot13(p[i]) | |
} | |
return | |
} | |
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