-
Notifications
You must be signed in to change notification settings - Fork 12
/
ioInterface.go
97 lines (83 loc) · 1.43 KB
/
ioInterface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"bufio"
"fmt"
"io"
)
type S1 struct {
F1 int
F2 string
}
type S2 struct {
F1 S1
text []byte
}
// Using pointer to S1 for changes to be persistent when the method exits
func (s *S1) Read(p []byte) (n int, err error) {
fmt.Print("Give me your name: ")
fmt.Scanln(&p)
s.F2 = string(p)
return len(p), nil
}
func (s *S1) Write(p []byte) (n int, err error) {
if s.F1 < 0 {
return -1, nil
}
for i := 0; i < s.F1; i++ {
fmt.Printf("%s ", p)
}
fmt.Println()
return s.F1, nil
}
func (s S2) eof() bool {
return len(s.text) == 0
}
func (s *S2) readByte() byte {
// this function assumes that eof() check was done before
temp := s.text[0]
s.text = s.text[1:]
return temp
}
func (s *S2) Read(p []byte) (n int, err error) {
if s.eof() {
err = io.EOF
return
}
l := len(p)
if l > 0 {
for n < l {
p[n] = s.readByte()
n++
if s.eof() {
s.text = s.text[0:0]
break
}
}
}
return
}
func main() {
s1var := S1{4, "Hello"}
fmt.Println(s1var)
buf := make([]byte, 2)
_, err := s1var.Read(buf)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Read:", s1var.F2)
_, _ = s1var.Write([]byte("Hello There!"))
s2var := S2{F1: s1var, text: []byte("Hello world!!")}
// Read s2var.text
r := bufio.NewReader(&s2var)
for {
n, err := r.Read(buf)
if err == io.EOF {
break
} else if err != nil {
fmt.Println("*", err)
break
}
fmt.Println("**", n, string(buf[:n]))
}
}