Created
January 30, 2019 21:10
-
-
Save yuseinishiyama/4613a7591ecce070ee36da15cf7c814b to your computer and use it in GitHub Desktop.
A Tour of Go: 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
// https://tour.golang.org/methods/23 | |
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (r *rot13Reader) Read(b []byte) (int, error) { | |
n, e := r.r.Read(b) | |
for i, v := range b[:n] { | |
switch { | |
case 'A' <= v && v <= 'M' || 'a' <= v && v <= 'm': | |
b[i] += 13 | |
case 'N' <= v && v <= 'Z' || 'n' <= v && v <= 'z': | |
b[i] -= 13 | |
} | |
} | |
return n, e | |
} | |
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