Svtter
1/6/2017 - 11:43 AM

https://tour.go-zh.org/methods/12

package main

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

type rot13Reader struct {
	r io.Reader
}

func (rot *rot13Reader) Read(b []byte) (int, error) {

	n, err := rot.r.Read(b)
	for i := 0; i < len(b); i++ {
		v, ok := Map[b[i]]
		if ok {
			b[i] = v
		}
	}
	return n, err
}

var Map map[byte]byte

func init_map() {
	Map = make(map[byte]byte)
	input := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	output := "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
	for n, _ := range input {
		Map[input[n]] = output[n]
	}
}

func main() {
	init_map()
	s := strings.NewReader("Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)
}