package main import ( "io" "os" "strings" ) type rot13Reader struct { r io.Reader } func rot13(p byte) byte { switch { case p >= 'A' && p <= 'M': p = p + 13 case p >= 'N' && p <= 'Z': p = p - 13 case p >= 'a' && p <= 'm': p = p + 13 case p >= 'n' && p <= 'z': p = p - 13 } 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( "purakha") r := rot13Reader{s} io.Copy(os.Stdout, &r) }
有疑问加站长微信联系(非本文作者)