Created
November 17, 2018 02:47
-
-
Save fr-xiaoli/23905c2adadfeb4f36f39e632325f889 to your computer and use it in GitHub Desktop.
A Tour of Go - Methods and interfaces - Exercise: rot13Reader
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 { | |
| r io.Reader | |
| } | |
| func (rot rot13Reader) Read(b []byte) (int, error) { | |
| l, err := rot.r.Read(b) | |
| if err != nil { return 0, err } | |
| for i, c := range b { | |
| if c >= 'A' && c <= 'M'|| (c >= 'a' && c <= 'm') { | |
| b[i] = c + 13 | |
| } else if c > 'M' && c <= 'Z' || (c > 'm' && c <= 'z') { | |
| b[i] = c - 13 | |
| } | |
| } | |
| return l, 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