-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasic_Reader_Writer.go
62 lines (49 loc) · 1.14 KB
/
Basic_Reader_Writer.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
//Basic Reader and Writer in Go
package main
import (
"fmt"
"io"
"log"
"os"
)
// myReader defines an io.Reader to read from stdin
type myReader struct{}
//Read reads data from stdin
func (myotherReader *myReader) Read(b []byte) (int, error) {
fmt.Print("in > ")
return os.Stdin.Read(b)
}
// myWriter defines an io.Writer to write to Stdout
type myWriter struct{}
// Write writes data to Stdout
func (myOtherWriter *myWriter) Write(b []byte) (int, error) {
fmt.Print("out > ")
return os.Stdout.Write(b)
}
func main() {
//Instansiate Reader and Writer
var (
reader myReader
writer myWriter
)
if _, err := io.Copy(&writer, &reader); err != nil {
log.Fatalln("Unable to read/write data")
}
//Alternative to code below
/*
//create a buffer to hold input /output
input := make([]byte, 4096)
//Use reader to read input
s, err := reader.Read(input)
if err != nil {
log.Fatalln("Unable to read data")
}
fmt.Printf("Read %d bytes from stdin\n", s)
//Use Writer to write output
s, err = writer.Write(input)
if err != nil {
log.Fatalln("Unable to write data")
}
fmt.Printf("Wrote %d bytes to stdout\n", s)
*/
}