golang rot13Reader

前端之家收集整理的这篇文章主要介绍了golang rot13Reader前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main

import (
	"io"
	"os"
	"strings"
)

type rot13Reader struct {
	r io.Reader
}

func (rot13 rot13Reader) Read(b []byte) (int,error) {
	n,err := rot13.r.Read(b)
	for i := 0; i < n; i++ {
		t := b[i]
		//lower char
		if t >= 97 && t <= 122 {
			if 122-t >= 13 {
                                b[i] = t + 13
                        } else {
				b[i] = t - 13
			}
		}
		//upper char
		if t >= 65 && t <= 90 {
			if 90-t >= 13 {
				b[i] = t + 13
			} else {
				b[i] = t - 13
			}
		}
	}

	return n,err
}

func main() {
        s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout,&r)
}
原文链接:https://www.f2er.com/go/190554.html

猜你在找的Go相关文章