This is a creation in Article, where the information may have evolved or changed.
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 Rot13substitution CI Pher to all alphabetical characters.
The rot13Reader type is provided. Make it a by io.Reader implementing it Read method.
Encryption method: Caesar encryption, key is 13
Package Main
Import (
"IO"
"OS"
"Strings"
)
Type Rot13reader struct {
R IO. Reader
}
Func Rot13 (b byte) byte {
Switch {
Case ' A ' <= b && b <= ' M ':
B = B + 13
Case ' M ' < b && B <= ' Z ':
b = b-13
Case ' A ' <= b && b <= ' m ':
B = B + 13
Case ' m ' < b && b <= ' z ':
b = b-13
}
Return b
}
Func (read *rot13reader) read (p []byte) (n int, err error) {
N, err = Read.r.read (p)
For I, Value: = Range p {
P[i] = Rot13 (value)
}
Return
}
Func Main () {
S: = Strings. Newreader (
"Lbh Penpxrq gur pbqr!")
R: = Rot13reader{s}
Io. Copy (OS. Stdout, &r)
}