Skip to content

Commit cd09dfd

Browse files
committed
Added reader interface exercises, along with rot13Exercise & image generator exercise
1 parent f04b2eb commit cd09dfd

File tree

9 files changed

+136
-0
lines changed

9 files changed

+136
-0
lines changed

go_basics/go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use (
1212
./methods
1313
./numeric_constants
1414
./pointers
15+
./reader
1516
./structs
1617
./typeConversionsAndInferrence
1718
./variables
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"image"
5+
"image/color"
6+
"math/rand"
7+
8+
"golang.org/x/tour/pic"
9+
)
10+
11+
type MyImage struct {
12+
X, Y int
13+
}
14+
15+
func (img MyImage) Bounds() image.Rectangle {
16+
return image.Rect(0, 0, img.X, img.Y)
17+
}
18+
19+
func (img MyImage) ColorModel() color.Model {
20+
return color.RGBAModel
21+
}
22+
23+
func (img MyImage) At(x, y int) color.Color {
24+
v := uint8(rand.Intn(255))
25+
// returning Red, Green random with blue fixed
26+
return color.RGBA{v, v, 255, 255}
27+
}
28+
29+
func main() {
30+
i := MyImage{X: 100, Y: 100}
31+
32+
pic.ShowImage(i)
33+
34+
}

go_basics/reader/customImages/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module anarchymonkey.com/go-basics/reader/customImages
2+
3+
go 1.20
4+
5+
require golang.org/x/tour v0.1.0

go_basics/reader/customImages/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE=
2+
golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc=

go_basics/reader/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module anarchymonkey.com/go-basics/reader
2+
3+
go 1.20
4+
5+
require golang.org/x/tour v0.1.0

go_basics/reader/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE=
2+
golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc=

go_basics/reader/reader.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"strings"
7+
8+
"golang.org/x/tour/reader"
9+
)
10+
11+
// Implement a Reader type that emits an infinite stream of the ASCII character 'A'.
12+
13+
type MyReader struct{}
14+
15+
// Reader is an interface that contains Read method, it takes a byte stream and then recieves the reader by itself and returns the length of bytes and the error if any
16+
func (m MyReader) Read(b []byte) (int, error) {
17+
18+
for i := range b {
19+
b[i] = 'A'
20+
}
21+
22+
return len(b), nil
23+
}
24+
25+
func main() {
26+
r := strings.NewReader("Hey my name is Aniket!")
27+
b := make([]byte, 8)
28+
for {
29+
lengthOfBytes, err := r.Read(b)
30+
fmt.Printf("n = %v err = %v b = %v\n", lengthOfBytes, err, b)
31+
fmt.Printf("b[:n] = %q\n", b[:lengthOfBytes])
32+
33+
if err == io.EOF {
34+
break
35+
}
36+
37+
}
38+
39+
// Custom reader implementation
40+
41+
fmt.Println("Validating the reader functionality")
42+
reader.Validate(MyReader{})
43+
}

go_basics/reader/rot13Exercise/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module anarchymonkey.com/go-basics/reader/rot13Exercise
2+
3+
go 1.20
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"io"
6+
"os"
7+
"strings"
8+
)
9+
10+
/*
11+
Implement a rot13Reader that implements io.Reader and reads from an io.Reader, modifying the stream by applying the rot13 substitution cipher to all alphabetical characters.
12+
13+
The rot13Reader type is provided for you. Make it an io.Reader by implementing its Read method.
14+
*/
15+
type rot13Reader struct {
16+
r io.Reader
17+
}
18+
19+
func (rot *rot13Reader) Read(b []byte) (int, error) {
20+
length, err := rot.r.Read(b)
21+
22+
if err != nil {
23+
return 0, errors.New("Some error occured while reading the file")
24+
}
25+
26+
for i := 0; i < length; i++ {
27+
if (b[i] >= 'A' && b[i] <= 'M') || (b[i] >= 'a' && b[i] <= 'm') {
28+
b[i] = b[i] + 13
29+
} else if (b[i] >= 'N' && b[i] <= 'Z') || (b[i] >= 'n' && b[i] <= 'z') {
30+
b[i] = b[i] - 13
31+
}
32+
}
33+
34+
return length, nil
35+
}
36+
37+
func main() {
38+
s := strings.NewReader("Lbh penpxrq gur pbqr!")
39+
r := &rot13Reader{s}
40+
io.Copy(os.Stdout, r)
41+
}

0 commit comments

Comments
 (0)