Skip to content

Instantly share code, notes, and snippets.

@futureperfect
Last active December 11, 2017 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futureperfect/6b8f612643f4cbf3d3c8b2025682f87c to your computer and use it in GitHub Desktop.
Save futureperfect/6b8f612643f4cbf3d3c8b2025682f87c to your computer and use it in GitHub Desktop.
Go Tour Implementations https://tour.golang.org
// https://tour.golang.org/methods/22
package main
import "golang.org/x/tour/reader"
type MyReader struct{}
func (reader MyReader) Read(b []byte) (int, error) {
count := 0
for i, _ := range b {
b[i] = 'A'
count += 1
}
return count, nil
}
func main() {
reader.Validate(MyReader{})
}
// https://tour.golang.org/methods/23
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r13 rot13Reader) Read(b []byte) (int, error) {
count, err := r13.r.Read(b)
if err != nil {
return count, err
}
for i := 0; i < count; i++ {
b[i] = rot13(b[i])
}
return count, nil
}
func rot13(b byte) byte {
switch {
case b >= 'A' && b <= 'Z':
return (((b - 'A') + 13) % 26) + 'A'
case b >= 'a' && b <= 'z':
return (((b - 'a') + 13) % 26) + 'a'
default:
return b
}
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment