-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_reader2.go
66 lines (57 loc) · 1.28 KB
/
go_reader2.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
/*
* Module: go_reader2.go
* Purpose: second reader example
* Date: N/A
* Notes:
* 1) To build:
* go build go_reader2.go
* 2) Ref: n/a
* 3) This uses buffers and writes to buffers
*
*/
package main
import (
"fmt"
"io"
//"strings"
"bytes"
)
// make writer function to write to its output
func fw(w io.Writer) {
fmt.Fprintln(w, "Writer Function Wrote This")
}
// make writer function that copies input
func fc(s string) *bytes.Buffer{
b := bytes.NewBufferString(s)
return b
}
func main() {
fmt.Println("go_reader2: Go reader 2 example")
// make buffer and use writer to write to it, then
// convert it to a string
var bb bytes.Buffer
fw(&bb)
s0 := bb.String()
fmt.Println("S0=", s0)
// make and print input string
s := "This is a test string that will be accessed"
sl := len(s)
fmt.Println("S (len=", sl, ") = ", s)
// make buffer from string
// -- not as efficient as strings.NewReader()
bs := bytes.NewBufferString(s)
fmt.Println("BS=", bs.String())
// func call to make buffer from string
ba := fc(s)
fmt.Println("BA=", ba.String())
// use the read method on the buffer
b := make([]byte, 8)
for {
n, err := ba.Read(b)
fmt.Printf("n = %v err = %v b = %v\n", n, err, b)
fmt.Printf("b[:n] = %q\n", b[:n])
if err == io.EOF {
break
}
}
}