Last active
December 11, 2017 02:06
-
-
Save futureperfect/6b8f612643f4cbf3d3c8b2025682f87c to your computer and use it in GitHub Desktop.
Go Tour Implementations https://tour.golang.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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{}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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