1
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (rot *rot13Reader) Read(p []byte) (n int, err error) {
n, err = rot.r.Read(p) //<---- where allocated the mem for p?
for i := 0; i < len(p); i++ {
if p[i] >= 'A' && p[i] <= 'Z' {
p[i] = 65 + (p[i] - 65 + 13) % 26
} else if p[i] >= 'a' && p[i] <= 'z' {
p[i] = 97 + (p[i] - 97 + 13) % 26
}
}
return
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
這是A行程的一個練習。上面的代碼可以正常運行,但我不明白在哪裏分配了p的內存。分配給內存的內存在哪裏?