A tour of Go Exercise: rot13 Reader
A common pattern is an IO. Reader that wraps anotherIo. Reader
,
Modifying the stream in some way.
For example, the gzip. newreader function takesIo. Reader
(
Stream of gzipped data) and returns* Gzip. Reader
That also implementsIo. Reader
(A stream of the decompressed data ).
ImplementRot13reader
That implementsIo. Reader
And reads fromIo. Reader
,
Modifying the stream by applying the rot13 substitution cipher to all alphabetical characters.
TheRot13reader
Type is provided for you. Make itIo. Reader
By implementing
ItsRead
Method
package mainimport ("Io" "OS" "strings") type rot13reader struct {r Io. reader} func rot13 (P byte) byte {Switch {case P> = 'A' & P <= 'M ': P = p-'A' + 'n' case P> = 'n' & P <= 'Z ': P = p-'n' + 'A' case P> = 'A' & P <= 'M ': P = p-'A' + 'n' case P> = 'n' & P <= 'Z ': P = p-'n' + 'A'} return p} func (V rot13reader) read (P [] Byte) (N int, err error) {Original: = make ([] B Yte, 50) I, err: = v. r. read (original) for index, value: = range original [: I] {P [Index] = rot13 (value)} return I, err} func main () {s: = strings. newreader ("LBH penpxrq Gur pbqr! ") R: = rot13reader {s} io. Copy (OS. stdout, & R)}