A tour of Go: Exercise: rot13 Reader

Source: Internet
Author: User
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. ReaderThat also implementsIo. Reader(A stream of the decompressed data ).

ImplementRot13readerThat implementsIo. ReaderAnd reads fromIo. Reader,
Modifying the stream by applying the rot13 substitution cipher to all alphabetical characters.

TheRot13readerType is provided for you. Make itIo. ReaderBy implementing
ItsReadMethod

 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)} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.