This is a creation in Article, where the information may have evolved or changed.
A Tour of Go
Exercise:rot13 Reader
A common pattern is an IO. Reader that wraps another io.Reader , modifying the stream in some.
For example, the gzip. Newreader function takes an io.Reader (a stream of gzipped data) and returns a that *gzip.Reader also implements io.Reader (a stream of t He decompressed data).
Implement a rot13Reader , implements io.Reader and reads from an io.Reader , modifying the stream by applying the ROT13 substitution ci Pher to all alphabetical characters.
The rot13Reader type is provided. Make it a by io.Reader implementing it Read 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 ' &&A mp 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 ([]byte, 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)}