Created
October 9, 2017 00:10
-
-
Save brianjester/276db3d90a8927c62aea621aa38c3d1a to your computer and use it in GitHub Desktop.
A Tour of Go - Methods and Interfaces - Exercise: rot13Reader (23/26)
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 < 'm' && b >= 'a' ) || (b < 'M' && b >= 'A')) { | |
b += 13 | |
} else if ((b > 'm' && b <= 'z') || ( b > 'M' && b <= 'Z')) { | |
b -= 13 | |
} | |
return b | |
} | |
func (rot rot13Reader) Read(p []byte) (n int, err error) { | |
n, err = rot.r.Read(p) | |
for i:=0;i<len(p);i++ { | |
p[i] = Rot13(p[i]) | |
} | |
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